One of hundreds of answers available with our premium content service.
A: The examples below uses three classes, VariableHost, VariableCaller and VariableSubclass, all in the same default package for simplicity. The VariableHost class has a static class variable and an instance variable and the cases show how they are accessed from the host class itself and the other classes.
public class VariableHost { static int staticInt = 2; int instanceInt = 4; public static void main(String[] args) { // Static reference int staticAccess = staticInt; // Object reference necessary for instance variable VariableHost hostInstance = new VariableHost(); int instanceAccess = hostInstance.instanceInt; } }
… full answer hidden
Premium members click below for full answer
How do I access a variable declared in another class?
Your premium content subscription is a single payment that gives you: