Java – what is the meaning of ‘static’ in the method header?

I want to know what is the function of the word 'static' in the title of the 'staticnumbers' method

public class DisplayClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        writeNumbers();
    }

    public static void writeNumbers()
    {
        int count;
        for(count=1; count<=20; count++)
        {
            System.out.println(count);
        }
    }
}

Solution

The term static means that the method is available at the class level, so there is no need to instantiate the object before calling it

Because writenumbers is called from a static method, it can only call other static methods, unless it first instantiates the new object of displayclass with the following methods:

DisplayClass displayClass = new DisplayClass();

Non static methods can only be called after instantiating this object, for example:

displayClass.nonStaticMethod();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>