方法头中'static'的含义是什么? [英] what is the meaning of 'static' in a method header?

查看:247
本文介绍了方法头中'static'的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想理解'staticNards'方法标题中'static'这个词的作用是什么?:

I want to understand what does the word 'static' do in the 'writeNumbers' method header?:

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);
        }
    }
}


推荐答案

术语 static 表示该方法在类级别可用,因此不需要在调用对象之前对其进行实例化。

The term static means that the method is available at the Class level, and so does not require that an object is instantiated before it's called.

因为 writeNumbers 是从一个本身 static 的方法调用的,所以它可以只调用其他静态方法,除非它首先使用以下内容实例化 DisplayClass 的新对象:

Because writeNumbers was being called from a method that was itself static it can only call other static methods, unless it first instantiates a new object of DisplayClass using something like:

DisplayClass displayClass = new DisplayClass();

只有在实例化此对象后才能调用非静态方法,例如:

only once this object has been instantiated could non-static methods been called, eg:

displayClass.nonStaticMethod();

这篇关于方法头中'static'的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆