0

I'm trying to understand scope of variables and how to use variables initialised from one class and use them in another but coming up stuck in my understanding.

I have a class Test1 defined as follows:

public class Test1 {

    private int x;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }
}

I am calling this class in my main:

public class Main {

    public static void main(String[] args) {
    Test1 test1 = new Test1();
    Test2 test2 = new Test2();

    test1.setX(33);
    int y = test1.getX();
    System.out.print(y);  // prints 33

    test2.testing(); // method outputs 0 instead of showing the value of X

    }
}

I have another class Test2:

public class Test2 {
    public static void testing() {
        Test1 test1 = new Test1();

        int val = test1.getX();
        System.out.print(val);

    }
}

If I then call the method in my main, the value of val is showing 0 rather than 33. How can I access the value in memory of getX() in another class?

Thanks

arsenal88
  • 1,040
  • 2
  • 15
  • 32
  • you have to used static modifier. private static int x; – Peter1982 Jun 25 '18 at 19:13
  • The point is that you don't understand the difference between *static* fields ... and ordinary fields. Unless a field is marked static, each object instances has its **own** copy. – GhostCat Jun 25 '18 at 19:18
  • 1
    You have *two* `Test1` objects. Consider an analogy... A car rolls off of an assembly line. You open the trunk of that car and put your suitcase in it. Moments later, another car rolls off of the assembly line. It is identical to the first car in every way. When you open the trunk of the second car, do you expect to find your suitcase there? Why not? – David Jun 25 '18 at 19:19
  • I do not think this is a duplicate of that. The duplicate flag answers another problem with the code, but does not actually answer the asker's question. – Zephyr Jun 25 '18 at 19:21
  • Creating a simple constructor in `Test2` to accept `test1` as a parameter would answer the question. – Zephyr Jun 25 '18 at 19:22
  • Ah ok I see. Yes you would not expect your suitcase there because the 2nd car is another object in itself – arsenal88 Jun 25 '18 at 19:22

2 Answers2

1

Think of classes as "molds" for creating new objects.

In your main method, you use that mold to create a new Test1 object. In the testing method, you use that same mold to create a new Test1 object. However, in that first object you made, you set the of x to 33, but did not do the same thing with that other object.

Think about it like a basketball. You have a mold to make basketballs and use it to make two basketballs. You pump up the first basketball to be 33% full of air, but don't put any air into the second one. Just because the same mold was used, does not mean both basketballs are pumped up to 33% air.

Edit: Here is a good video explaining scope that should help you out! https://www.youtube.com/watch?v=Y2iN3TO5qOQ

Michael Ziluck
  • 599
  • 6
  • 19
0

The reason you are seeing 0 is because you are creating a new instance of Test1 and referencing that in your getX() call.

In order to see the value of an existing instance of your class you would have to pass that instance into your method. For example:

public void testing(Test1 instance){
    System.out.println(instance.getX());
}
John Kane
  • 4,383
  • 1
  • 24
  • 42