Static method in java

Static method in java

Your question: is the statement static methods should use only static variables correct?

No. The statement is not correct.

The correct statement will be static methods can only use those instance variables that are defined static

Take a look at following code and read the comments:

Class A{
    int i;
    static int j;

    public static void methodA(){
        i = 5; //cannot use since i is not static
        j = 2; //can use.

        int k = 3; //k is local variable and can be used no problem

        **EDIT:**//if you want to access i
        A a = new A();
        //A.i = 5; //can use.  
        a.i = 5; // it should be non-capital a right?
    }
}

First of all, a technicality: its NOT true that main method is also static. You can define a non-static main method, with whatever signature you choose; it just wont be a valid Java application entry point.

With regards to static methods should use only static variables, this is also NOT true. The key concept here is that static methods and fields are class-specific, not instance-specific. You simply cant access an instance variable/method if you dont actually have an instance of that class; its a compilation error.

So to be precise, without an instance, you cant access instance fields/methods. You can access static fields/methods without an instance. If you need to access instance fields/methods from a static method, you have to get an instance of that class one way or another, either by simply instantiating it, or by getting a reference to it from a static field or method parameter.

Lets take a look at this simple example:

public static void main(String args[]) {
  System.out.println(args.length);
}

length is NOT a static field; its an instance field of array instances, which args is. The static method main is able to get this instance (and thus access its instance methods and fields) because its passed in as an argument.

Also, println is NOT a static method; its an instance method of PrintStream instances. The static method main is able to get this instance by accessing the static field out of the class System.


To summarize:

  • A Java application entry point is a method that:
    • is named main
    • is public and static
    • returns void and takes a String[] argument as parameter
  • A method named main doesnt have to be a Java application entry point
    • (but its best to reserve this name for that purpose)

Furthermore,

  • Instance fields/methods can only be accessed through an instance
  • Static fields/methods can be accessed without an instance
  • Static methods can get an instance of a class by one of the following ways:
    • creating a new instance
    • having it passed as an argument
    • accessing it through a static field of a class
    • accepting it as the return value of a static method of a class
    • catching it as a thrown Throwable

Static method in java

Maybe this piece of code will enlighten you:

public class Main {

    private String instanceField = Joe;

    private void instanceMethod() {
        System.out.println(Instance name= + instanceField);
    }

    public static void main(String[] args) {

        // cannot change instance field without an instance
        instanceField = Indy; // compilation error

        // cannot call an instance method without an instance
        instanceMethod(); // compilation error

        // create an instance
        Main instance = new Main();

        // change instance field
        instance.instanceField = Sydney;

        // call instance method
        instance.instanceMethod();  
    }
}

So you cannot access instance members without an instance. Within the context of a static method you dont have a reference to an instance unless you receive or create one.

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *