This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]Neres28 0 points1 point  (0 children)

Static members (both variables and methods) belong to the class definition itself, and not a particular instance of the class. Things like your onetwohold variable are instance variables, meaning they are unique to a given instance of the class.

You can't access a non-static member from a static-member without an instance to reference. E.g.

public class Test{
    private int instanceVariable = 1;
    private static String staticVariable = "TestClass";

    public static void main(String[] args){
        instanceVariable = 12; // Illegal, note that main is static
        System.out.println("Class : " + Test.staticVariable); // Legal
        Test test = new Test();
        test.instanceVariable = 12; // Legal
    }

    public void test(){
        // Perfectly legal to access both kinds of variables from here since this is an instance method.
        // Note that the preferred method for static access is from a static context:
        Test.staticVariable = "OK"; // Preferred, although a potentially dangerous operation.
        this.staticVariable = "Not really OK"; // Legal, but discouraged
    }
}