Yes, it is possible. Assuming index of the class classA is also package protected and classA is in the same package as classB then you can do something like that:
class classA {
int index;
classA() {}
// OR
classA(int index) {
this.index = index;
}
}
class classB {
int indexB;
classA classA;
classB() {
indexB = 0;
classA = new classA();
classA.index = indexB;
// OR
classA = new classA(indexB);
}
}
If both classes are in different packages you then you need a public setter for index in classA as demonstrated by ssantos (The second method I've shown, using the constructor, will work in this case too it the constructor is public).
It is however not very good to access members of other classes directly. Please use getters and setters for that, also please consider the java naming conventions: class should start with an uppercase letter, thus ClassA and ClassB.
javais tagged with question