You use getters and setters.
class A {
private int a_number;
public int getNumber() { return a_number; }
}
class B {
private int b_number;
public void setNumber(int num) { b_number = num; }
}
.. And in your main method, wherever it is:
public static void main(String[] args) {
A a = new A();
int blah = a.getNumber();
B b = new B();
b.setNumber(blah);
}
You can also use constructors as a means of an "initial setter" so that the object is always created with a minimum set of variables already instantiated, for example:
class A {
private int a_number;
public A(int number) { // this is the only constructor, you must use it, and you must give it an int when you do
a_number = number;
}
public int getNumber() { return a_number; }
}
Multiplynot be made static? As for your question in general, it might be easier if you distinguish between "class" and "object." You're not passing a variable "to the same class." You're create an object of typeTestMeand calling a function on it. You can create an object of any other type you'd like and call a function on that exactly the same way.