CSS font stacks & developer FAQs with Web standards
Site navigation below

One of hundreds of answers available with our premium content service.

Part answer, see below for more

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

Click for full answers

Premium members click below for full answer
How do I access a variable declared in another class?

Premium service benefits

Your premium content subscription is a single payment that gives you:

Home · Web fonts · Font stacks · FAQs · Java · CSS · Javascript · HTML · Site manager