通俗地说,Java 中的“静态"是什么意思? [英] In laymans terms, what does 'static' mean in Java?

查看:31
本文介绍了通俗地说,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?

推荐答案

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天全站免登陆