Q:

Is it possible to call another classes method variables from the main class?

i have recently started programming in Java and have run into a problem that im not too sure of how to search up an answer for. So heres the short version:
I have a class called test(for the sake of the question) with public variables and public methods which have their own variables. In my main class i can create an object to call the test class variables, but can i call the variables that have been used in the methods from that class?

A:

No, you would have to store that variable as a member variable of the class. For example:
public class Test {
    public int b;

    Test() {
        int a = 1;
        b = 2;
    }
}

So in your other class, you could get the value of b like this:
Test test = new Test();
int valueOfBInTest = test.b;

But there is no way to retrieve the a variable that was used in the constructor of Test because that variable was forgotten at the end of the Test constructor. This is because of something called scope. The b variable has a scope of the instance of a class, so as long as that instance of the Test class is around, you can retrieve b. But since the a variable is declared in the Test constructor, it is only available in the constructor of Test. To be clear, the same thing will happen in any method of Test. If you declare a variable inside the method, that variable will be lost when that method is finished.

