Static Methods Part 1 Tutorial

In my Introduction to the Static Modifier tutorial, I discussed how the static modifier allows a class variable to be called directly. The concept worked great in that tutorial because the main method always contains the static modifier. There are many rules to understand when applying the static modifier to class members. Here are a few:

  • A static method cannot directly access a non-static instance variable. An instance variable can only be accessed inside of a static method by reference.
  • A static method can directly access a static class variable from within the same class.
  • A static method can directly access a static class variable from another class.
  • A static method cannot directly invoke a non-static method from within the same class. A non-static method can only be invoked by reference.
  • A static method can directly invoke a static method from within the same class.
  • A static method can directly invoke a static method from another class.
  • A static method cannot use the this keyword.


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StaticMethodOne
C:\Java>cd StaticMethodOne
C:\Java\StaticMethodOne>Notepad StaticMethodOne.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class StaticMethodOne {
    int instanceInt = 0; // non-static instance variable
    static int staticInt = 0; // static class variable

    public static void main(String args[]) {
        // instanceInt = 10; // no! A static method cannot directly access a non-static instance variable
        StaticMethodOne smo = new StaticMethodOne();
        smo.instanceInt = 10; // only access via reference
        System.out.println("Non-static instanceInt value via reference = " + smo.instanceInt);
        
        /////
        staticInt = 20; // yes! A static method can directly access a static class variable from the same class.
        System.out.println("Static staticInt = " + staticInt);

        /////
        OtherClass.sharedInt = 41; // yes! A static method can directly access a static class variable from another class
        System.out.println("Static sharedInt from OtherClass = " + OtherClass.sharedInt);

        /////
        // nonStaticMethod(); // no! A static method cannot directly invoke a non-static method
        smo.nonStaticMethod(); // only invoke via reference

        /////
        staticMethod(); // A static method can directly invoke a static method from within the same class.
		
        /////
        OtherClass.otherStaticMethod(); // A static method can directly invoke a static method from another class.

        // this.staticMethod(); // cannot use the this keyword.
        // this.nonStaticMethod(); // cannot use the this keyword.
        // this.instanceInt = 6; // cannot use the this keyword.
        // this.staticInt = 5; // cannot use the this keyword.

    }
    void nonStaticMethod () { 
        System.out.println("nonStaticMethod invoked using reference"); 
    }

    static void staticMethod () { 
        System.out.println("staticMethod invoked directly"); 
    }
}
class OtherClass {
    static int sharedInt = 0;

    static void otherStaticMethod() {
        System.out.println("OtherClass.otherStaticMethod invoked directly");
    }
}

Now switch back to the command prompt (CMD) and type in javac StaticMethodOne.java and press Enter.
Now type in java StaticMethodOne and press Enter.


C:\Java\StaticMethodOne>javac StaticMethodOne.java
C:\Java\StaticMethodOne>java StaticMethodOne
Non-static instanceInt value via reference = 10
Static staticInt = 20
Static sharedInt from OtherClass = 41
nonStaticMethod invoked using reference
staticMethod invoked directly
OtherClass.otherStaticMethod invoked directly


Final thoughts

Coming up next is part 2 of this tutorial where I will discuss how non-static methods can access static class variables and invoke static methods.


Tutorials