5

Lets say I have 3 Classes: A, Data, and B

I pass a variable from class A which sets that passed variable to a private variable in class Data.

Then in class B, I want to call that specific variable which has been changed.

So I do

Data data = new Data();
data.getVariable();

It will then return null, since in class Data I initialize variables to nothing (ex: int v;), and I think that class B is initializing a brand new class and resetting the values to default, but I don't know how to fix this.

I know that the variable is setting properly because in class A if I do data.getVariable() it will print the variable that was set.

Class A:

Data data = new Data();
int d = 1;
data.setVariable(d);

Class Data:

private static int b;

public void setVariable(int s)
{
    b = s;
}

public int getVariable()
{
    return b;
}

Class B:

Data data = new Data();
private int v; 

v = data.getVariable();
System.out.println(v);

This will print out 0 instead of the actual value

Kevin
  • 53,822
  • 15
  • 101
  • 132
user1062898
  • 109
  • 2
  • 2
  • 10
  • 2
    Please provide your code, not just descriptions of your classes. – James DW Nov 23 '11 at 22:53
  • My code for all 3 classes is over 800 lines long, so I just made up what I currently test in it... I don't know what extends does or not, so if you could explain better and if it would solve my solution, then I will try it. – user1062898 Nov 23 '11 at 22:58
  • If I Do extends Data in class B, it still doesn't work – user1062898 Nov 23 '11 at 23:07
  • The class B definition would not ever compile. Show the code please. – alf Nov 23 '11 at 23:24
  • All it is missing is public class.. – user1062898 Nov 23 '11 at 23:27
  • Please post your `main` method – Kevin Nov 23 '11 at 23:46
  • You need to either (a) reference the same instance, or (b) make the variable static. – Dave Newton Nov 23 '11 at 23:49
  • Are you sure that your request are happening in the correct order? You are sure that class A is setting the variable, before class B reads it? – dann.dev Nov 23 '11 at 23:50
  • My problem was assigning the variables after static. – user1062898 Nov 24 '11 at 00:14
  • I think I did the same thing as was suggested in the accepted answer but I still get different values for the static variable. I have a variable called `x` in a class `SharedUtils` whose default value is `-1` . I have set `SharedUtils.x = 1` at one place but later when I check the value of the variable, it shows the default value of `-1` and not the value I've set it to. Am I missing something here? – Durga Swaroop Dec 19 '16 at 16:20

2 Answers2

11

When you instantiate a Data object in class A, and instantiate another Data object in class B, they are two different instances of the Data class. They both instantiate d to 0 by default. You then call setVariable on the instance in class A and pass it the value of 1; but the instance in class B remains in 0. In order to change the value of the instance in class B, you would need to call setVariable on the instance in class B.

What it seems like you're looking for is a static member. Static members are the same across all instances of the same class. Just put the static keyword before the method(s) or field(s) that you want to use it. Static members and fields are typically accessed using the name of the class in which they are declared (i.e. MyClass.doMethod()). For example:

Class Data (updated):

private static int b;

public static void setVariable(int s)
{
    b = s;
}

public static int getVariable()
{
    return b;
}

Class A:

Data.setVariable(d);

Class B:

v = Data.getVariable();
System.out.println(v);
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • 3
    The methods dont need to be static. He can still use "new" and have the methods non static. The variable is enough – Raul Guiu Nov 24 '11 at 00:02
  • Even putting static before the methods, it will still give me 0. – user1062898 Nov 24 '11 at 00:07
  • Alright, I seemed to have fixed my problem where I was declaring the variables and assigning them to get the data. – user1062898 Nov 24 '11 at 00:11
  • @Avempace: While technically "correct," I would consider that to be bad design. – Jon Newmuis Nov 24 '11 at 01:00
  • I think I did the same thing. I have a variable called `x` in a class `SharedUtils` whose default value is `-1` . I have set `SharedUtils.x = 1` at one place but later when I check the value of the variable, it shows the default value of `-1` and not the value I've set it to. Am I missing something here? – Durga Swaroop Dec 19 '16 at 15:43
1

Editing - my first suggestion was to use static for variable b, and the author changed his question adding that suggestion.

It fixes what you are trying to do. I write the example in code that compiles:

    public class Demo {
        public static void main(String[] args) {
            A a = new A();
            B b = new B();
            a.doWhatever();
            b.doSomethingElse();
        }
    }
    class Data {
        private static int b;

        public void setVariable(int s)
        {
            b = s;
        }

        public int getVariable()
        {
            return b;
        }
    }
    class A {
        public void doWhatever() {
            Data data = new Data();
            int d = 1;
            data.setVariable(d);
        }
    }

    class B {
        Data data = new Data();
        private int v; 
        public void doSomethingElse() {
            v = data.getVariable();
            System.out.println(v);
        }
    }
Raul Guiu
  • 2,374
  • 22
  • 37
  • This somehwat works, but when I print out V in Class B, it still displays null. I updated the main code again with static. – user1062898 Nov 23 '11 at 23:20
  • I am a bit confused. How can it print "null"? it is an integer – Raul Guiu Nov 23 '11 at 23:23
  • Ok, well I was making an example, in mine it is a string but it doesn't matter anyway since the integer will print out 0 instead of the actual value, my bad. So just replace null with 0. – user1062898 Nov 23 '11 at 23:24
  • It displays 0, not null. 0 is not the correct value anyway, it should be displaying 6. I can't print out the whole code because its way to long. – user1062898 Nov 23 '11 at 23:26
  • Not sure then, I looked over what I have and it is doing the exact same thing. I'll keep looking over yours to see if I am missing something. Thanks for the help – user1062898 Nov 24 '11 at 00:06