用外行人的话来说,Java中“静态”意味着什么? [英] In laymans terms, what does 'static' mean in Java?

查看:122
本文介绍了用外行人的话来说,Java中“静态”意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知了几个定义,在维基百科上看,但作为Java的初学者,我仍然不确定它的含义。是否有流利的Java和白痴?

I have been told several definitions for it, looked on Wikipedia, but as a beginner to Java I'm still not sure what it means. Anybody fluent in Java and idiot?

推荐答案

static意味着标记为此类的变量或方法在类级别可用。换句话说,您不需要创建类的实例来访问它。

static means that the variable or method marked as such is available at the class level. In other words, you don't need to create an instance of the class to access it.

public class Foo {
    public static void doStuff(){
        // does stuff
    }
}

所以,不要创建一个Foo实例,然后像这样调用 doStuff

So, instead of creating an instance of Foo and then calling doStuff like this:

Foo f = new Foo();
f.doStuff();

您只需直接针对该类调用该方法,如下所示:

You just call the method directly against the class, like so:

Foo.doStuff();

这篇关于用外行人的话来说,Java中“静态”意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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