Pass variable between class - java












0















I just started with Java recently. I have one variable in one class and I want to use in another class. I know that this is very basic, but if someone can help....



This is the class and the variable is value



public class MainApp {
.
.
.

public static class AnalogSampleListener implements IIOSampleReceiveListener {
@Override
public void ioSampleReceived(RemoteXBeeDevice remoteDevice, IOSample ioSample) {
for (IOLine line : LINES) {
if (ioSample.hasAnalogValue(line)) {
int ***value*** = ioSample.getAnalogValue(line);

System.out.println(new Timestamp(System.currentTimeMillis()) + " " + "Analog data from '" + remoteDevice.get64BitAddress() +
"': " + value);

}
}
.
.
}


I want to send the variable value to this class:



public class RandomWave implements Runnable{

.
.
.

public void run()
{
long currentTime = 0;
long nextTime = 0;

// Variable to keep track of the timing
long timer = System.nanoTime();

while (!stopThread)
{
// Compute the next data value
currentTime = (System.nanoTime() - timer) / 1000000;

double p = currentTime / 1000.0 * 4;
double series0 = ***HERE I NEED THE VARIABLE VALUE***;
double series1 = 210 + 60 * Math.sin(p / 21.7) * Math.sin(p / 7.8);

// Call the handler
handler.onData(currentTime / 1000.0, series0, series1);

// Sleep until next walk
if ((nextTime += interval) <= currentTime)
nextTime = currentTime + interval;

try { Thread.sleep((int)(nextTime - currentTime)); }
catch (InterruptedException e) {};
}
}


Thank you










share|improve this question




















  • 2





    what is stopping you, and what is your actual question?

    – Stultuske
    Nov 22 '18 at 12:10











  • I need to insert this variable: int value = ioSample.getAnalogValue(line); here: double series0 = HERE I NEED THE VARIABLE VALUE;

    – Homero Ferretti
    Nov 25 '18 at 10:29


















0















I just started with Java recently. I have one variable in one class and I want to use in another class. I know that this is very basic, but if someone can help....



This is the class and the variable is value



public class MainApp {
.
.
.

public static class AnalogSampleListener implements IIOSampleReceiveListener {
@Override
public void ioSampleReceived(RemoteXBeeDevice remoteDevice, IOSample ioSample) {
for (IOLine line : LINES) {
if (ioSample.hasAnalogValue(line)) {
int ***value*** = ioSample.getAnalogValue(line);

System.out.println(new Timestamp(System.currentTimeMillis()) + " " + "Analog data from '" + remoteDevice.get64BitAddress() +
"': " + value);

}
}
.
.
}


I want to send the variable value to this class:



public class RandomWave implements Runnable{

.
.
.

public void run()
{
long currentTime = 0;
long nextTime = 0;

// Variable to keep track of the timing
long timer = System.nanoTime();

while (!stopThread)
{
// Compute the next data value
currentTime = (System.nanoTime() - timer) / 1000000;

double p = currentTime / 1000.0 * 4;
double series0 = ***HERE I NEED THE VARIABLE VALUE***;
double series1 = 210 + 60 * Math.sin(p / 21.7) * Math.sin(p / 7.8);

// Call the handler
handler.onData(currentTime / 1000.0, series0, series1);

// Sleep until next walk
if ((nextTime += interval) <= currentTime)
nextTime = currentTime + interval;

try { Thread.sleep((int)(nextTime - currentTime)); }
catch (InterruptedException e) {};
}
}


Thank you










share|improve this question




















  • 2





    what is stopping you, and what is your actual question?

    – Stultuske
    Nov 22 '18 at 12:10











  • I need to insert this variable: int value = ioSample.getAnalogValue(line); here: double series0 = HERE I NEED THE VARIABLE VALUE;

    – Homero Ferretti
    Nov 25 '18 at 10:29
















0












0








0








I just started with Java recently. I have one variable in one class and I want to use in another class. I know that this is very basic, but if someone can help....



This is the class and the variable is value



public class MainApp {
.
.
.

public static class AnalogSampleListener implements IIOSampleReceiveListener {
@Override
public void ioSampleReceived(RemoteXBeeDevice remoteDevice, IOSample ioSample) {
for (IOLine line : LINES) {
if (ioSample.hasAnalogValue(line)) {
int ***value*** = ioSample.getAnalogValue(line);

System.out.println(new Timestamp(System.currentTimeMillis()) + " " + "Analog data from '" + remoteDevice.get64BitAddress() +
"': " + value);

}
}
.
.
}


I want to send the variable value to this class:



public class RandomWave implements Runnable{

.
.
.

public void run()
{
long currentTime = 0;
long nextTime = 0;

// Variable to keep track of the timing
long timer = System.nanoTime();

while (!stopThread)
{
// Compute the next data value
currentTime = (System.nanoTime() - timer) / 1000000;

double p = currentTime / 1000.0 * 4;
double series0 = ***HERE I NEED THE VARIABLE VALUE***;
double series1 = 210 + 60 * Math.sin(p / 21.7) * Math.sin(p / 7.8);

// Call the handler
handler.onData(currentTime / 1000.0, series0, series1);

// Sleep until next walk
if ((nextTime += interval) <= currentTime)
nextTime = currentTime + interval;

try { Thread.sleep((int)(nextTime - currentTime)); }
catch (InterruptedException e) {};
}
}


Thank you










share|improve this question
















I just started with Java recently. I have one variable in one class and I want to use in another class. I know that this is very basic, but if someone can help....



This is the class and the variable is value



public class MainApp {
.
.
.

public static class AnalogSampleListener implements IIOSampleReceiveListener {
@Override
public void ioSampleReceived(RemoteXBeeDevice remoteDevice, IOSample ioSample) {
for (IOLine line : LINES) {
if (ioSample.hasAnalogValue(line)) {
int ***value*** = ioSample.getAnalogValue(line);

System.out.println(new Timestamp(System.currentTimeMillis()) + " " + "Analog data from '" + remoteDevice.get64BitAddress() +
"': " + value);

}
}
.
.
}


I want to send the variable value to this class:



public class RandomWave implements Runnable{

.
.
.

public void run()
{
long currentTime = 0;
long nextTime = 0;

// Variable to keep track of the timing
long timer = System.nanoTime();

while (!stopThread)
{
// Compute the next data value
currentTime = (System.nanoTime() - timer) / 1000000;

double p = currentTime / 1000.0 * 4;
double series0 = ***HERE I NEED THE VARIABLE VALUE***;
double series1 = 210 + 60 * Math.sin(p / 21.7) * Math.sin(p / 7.8);

// Call the handler
handler.onData(currentTime / 1000.0, series0, series1);

// Sleep until next walk
if ((nextTime += interval) <= currentTime)
nextTime = currentTime + interval;

try { Thread.sleep((int)(nextTime - currentTime)); }
catch (InterruptedException e) {};
}
}


Thank you







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:20









Noushad

17027




17027










asked Nov 22 '18 at 12:09









Homero FerrettiHomero Ferretti

11




11








  • 2





    what is stopping you, and what is your actual question?

    – Stultuske
    Nov 22 '18 at 12:10











  • I need to insert this variable: int value = ioSample.getAnalogValue(line); here: double series0 = HERE I NEED THE VARIABLE VALUE;

    – Homero Ferretti
    Nov 25 '18 at 10:29
















  • 2





    what is stopping you, and what is your actual question?

    – Stultuske
    Nov 22 '18 at 12:10











  • I need to insert this variable: int value = ioSample.getAnalogValue(line); here: double series0 = HERE I NEED THE VARIABLE VALUE;

    – Homero Ferretti
    Nov 25 '18 at 10:29










2




2





what is stopping you, and what is your actual question?

– Stultuske
Nov 22 '18 at 12:10





what is stopping you, and what is your actual question?

– Stultuske
Nov 22 '18 at 12:10













I need to insert this variable: int value = ioSample.getAnalogValue(line); here: double series0 = HERE I NEED THE VARIABLE VALUE;

– Homero Ferretti
Nov 25 '18 at 10:29







I need to insert this variable: int value = ioSample.getAnalogValue(line); here: double series0 = HERE I NEED THE VARIABLE VALUE;

– Homero Ferretti
Nov 25 '18 at 10:29














2 Answers
2






active

oldest

votes


















0














You need to write a getter Method for this var. As an example i will do it with a String named "myString".



public String getMyString(){
return myString;
}


Now you can create an Instance of the other class an call this method to get the value:



MyClass c = new MyClass();
String valueFromTheOtherClass = c.getMyString();


I hope i understood you're question right...






share|improve this answer
























  • I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

    – Mick
    Nov 22 '18 at 12:19



















0














Value is a local variable and does not exist after "ioSampleReceived" finished.



But I like to do a very general explanation. If you need to read a value from another class, you can make it "public static". Static means that this variable does not exist for every object instance. There is only one. Living in the class, not in the object. It's quite simple for some other class to access this value. Just call "ClassA.value". But be aware of multi-threading issues which arise if your code is being executed in parallel.



It get's a bit more complicated if you have non-static values. There are many instances of ClassA and ClassB needs to be told which object instance of ClassA it has to use. Maybe have the instance of ClassA included in the constructor of ClassB. This also falls under the topic "Dependency Injection". Frameworks can help you, but after all, you need to tell ClassB about the ClassA object, before ClassB can read any values of ClassA.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430710%2fpass-variable-between-class-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You need to write a getter Method for this var. As an example i will do it with a String named "myString".



    public String getMyString(){
    return myString;
    }


    Now you can create an Instance of the other class an call this method to get the value:



    MyClass c = new MyClass();
    String valueFromTheOtherClass = c.getMyString();


    I hope i understood you're question right...






    share|improve this answer
























    • I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

      – Mick
      Nov 22 '18 at 12:19
















    0














    You need to write a getter Method for this var. As an example i will do it with a String named "myString".



    public String getMyString(){
    return myString;
    }


    Now you can create an Instance of the other class an call this method to get the value:



    MyClass c = new MyClass();
    String valueFromTheOtherClass = c.getMyString();


    I hope i understood you're question right...






    share|improve this answer
























    • I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

      – Mick
      Nov 22 '18 at 12:19














    0












    0








    0







    You need to write a getter Method for this var. As an example i will do it with a String named "myString".



    public String getMyString(){
    return myString;
    }


    Now you can create an Instance of the other class an call this method to get the value:



    MyClass c = new MyClass();
    String valueFromTheOtherClass = c.getMyString();


    I hope i understood you're question right...






    share|improve this answer













    You need to write a getter Method for this var. As an example i will do it with a String named "myString".



    public String getMyString(){
    return myString;
    }


    Now you can create an Instance of the other class an call this method to get the value:



    MyClass c = new MyClass();
    String valueFromTheOtherClass = c.getMyString();


    I hope i understood you're question right...







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 12:16









    SilvanSilvan

    609




    609













    • I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

      – Mick
      Nov 22 '18 at 12:19



















    • I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

      – Mick
      Nov 22 '18 at 12:19

















    I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

    – Mick
    Nov 22 '18 at 12:19





    I like to point out that getters and setters are "just" Java Bean style. For the purpose of demonstration, you can work with public attributes as well.

    – Mick
    Nov 22 '18 at 12:19













    0














    Value is a local variable and does not exist after "ioSampleReceived" finished.



    But I like to do a very general explanation. If you need to read a value from another class, you can make it "public static". Static means that this variable does not exist for every object instance. There is only one. Living in the class, not in the object. It's quite simple for some other class to access this value. Just call "ClassA.value". But be aware of multi-threading issues which arise if your code is being executed in parallel.



    It get's a bit more complicated if you have non-static values. There are many instances of ClassA and ClassB needs to be told which object instance of ClassA it has to use. Maybe have the instance of ClassA included in the constructor of ClassB. This also falls under the topic "Dependency Injection". Frameworks can help you, but after all, you need to tell ClassB about the ClassA object, before ClassB can read any values of ClassA.






    share|improve this answer




























      0














      Value is a local variable and does not exist after "ioSampleReceived" finished.



      But I like to do a very general explanation. If you need to read a value from another class, you can make it "public static". Static means that this variable does not exist for every object instance. There is only one. Living in the class, not in the object. It's quite simple for some other class to access this value. Just call "ClassA.value". But be aware of multi-threading issues which arise if your code is being executed in parallel.



      It get's a bit more complicated if you have non-static values. There are many instances of ClassA and ClassB needs to be told which object instance of ClassA it has to use. Maybe have the instance of ClassA included in the constructor of ClassB. This also falls under the topic "Dependency Injection". Frameworks can help you, but after all, you need to tell ClassB about the ClassA object, before ClassB can read any values of ClassA.






      share|improve this answer


























        0












        0








        0







        Value is a local variable and does not exist after "ioSampleReceived" finished.



        But I like to do a very general explanation. If you need to read a value from another class, you can make it "public static". Static means that this variable does not exist for every object instance. There is only one. Living in the class, not in the object. It's quite simple for some other class to access this value. Just call "ClassA.value". But be aware of multi-threading issues which arise if your code is being executed in parallel.



        It get's a bit more complicated if you have non-static values. There are many instances of ClassA and ClassB needs to be told which object instance of ClassA it has to use. Maybe have the instance of ClassA included in the constructor of ClassB. This also falls under the topic "Dependency Injection". Frameworks can help you, but after all, you need to tell ClassB about the ClassA object, before ClassB can read any values of ClassA.






        share|improve this answer













        Value is a local variable and does not exist after "ioSampleReceived" finished.



        But I like to do a very general explanation. If you need to read a value from another class, you can make it "public static". Static means that this variable does not exist for every object instance. There is only one. Living in the class, not in the object. It's quite simple for some other class to access this value. Just call "ClassA.value". But be aware of multi-threading issues which arise if your code is being executed in parallel.



        It get's a bit more complicated if you have non-static values. There are many instances of ClassA and ClassB needs to be told which object instance of ClassA it has to use. Maybe have the instance of ClassA included in the constructor of ClassB. This also falls under the topic "Dependency Injection". Frameworks can help you, but after all, you need to tell ClassB about the ClassA object, before ClassB can read any values of ClassA.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 12:18









        MickMick

        503310




        503310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430710%2fpass-variable-between-class-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]