下面的代码是如何工作的

发布于 2024-08-21 03:06:13 字数 244 浏览 5 评论 0原文

目前我正在尝试像这样调用它:

class Test {
    public static void test() {
        System.out.println("hi");
    }
    public static void main(String[] args) {
        Test t = null;
        t.test();
    }
}

代码的输出是 hi

Currently I'm trying to invoke it like this:

class Test {
    public static void test() {
        System.out.println("hi");
    }
    public static void main(String[] args) {
        Test t = null;
        t.test();
    }
}

The output of the code is hi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

萌辣 2024-08-28 03:06:13

尝试在点之前使用类名的 Test.test()

静态方法是在类本身而不是类的实例上调用的。

Try Test.test() with the class name before the dot.

Static methods are called on the class itself not instances of the class.

凉城 2024-08-28 03:06:13

您不需要实例化 Test 来调用静态方法。你的主要可能是这样的:

public static void main(String[] args) {
    Test.test();
}

You don't need to instantiate Test for calling a static method. Your main could be look like this:

public static void main(String[] args) {
    Test.test();
}
兲鉂ぱ嘚淚 2024-08-28 03:06:13

静态方法应该使用类名来调用,而不需要创建类的实例,如

ClassName.methodName(args);

methodName(args); // from other static methods of the same class.

您也可以使用对象引用来引用静态方法,例如,

instanceName.methodName(args)

但不鼓励这样做,因为它没有明确说明它们是类方法。

所以在你的情况下:

Test.test();

或者

test();

从 main 方法就可以了。

Static methiods should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args);

or

methodName(args); // from other static methods of the same class.

You can also refer to static methods with an object reference like

instanceName.methodName(args)

but this is discouraged because it does not make it clear that they are class methods.

So in your case:

Test.test();

or

test();

from the main method will do.

允世 2024-08-28 03:06:13

尝试:

Test.test();

Try:

Test.test();
难理解 2024-08-28 03:06:13

你们在同一个类中,只需从 main() 调用 test() 即可。

You are in the same class, you can simply call test() from main().

℡Ms空城旧梦 2024-08-28 03:06:13
for (Method m : Class.forName ("Test").getDeclaredMethods ()) {
   if (Modifier.isStatic (m.getModifiers ()) {
      m.invoke (null);
   }
}

只是为了lulz

for (Method m : Class.forName ("Test").getDeclaredMethods ()) {
   if (Modifier.isStatic (m.getModifiers ()) {
      m.invoke (null);
   }
}

just for lulz

余生一个溪 2024-08-28 03:06:13

静态方法和静态变量的好处是您不需要类的实例即可使用它们。

通常你会创建一个实例并调用该方法

Test myClass = new Text();
myClass.test();

但是对于静态方法第一行不是必需的,你只需要在开头写下类名

Test.test();

但是,在静态方法中你无法访问测试类中的任何实例变量- 除非它们也是静态的!

The good thing about static methods and static variables is that you do not need an instance of the class to use it.

Normally you would create an instance and call the method

Test myClass = new Text();
myClass.test();

However with static methods the first line is not necessary, You just need to write the Class name at the start

Test.test();

However, in static methods you are not able to access any instance variables inside the Test class - unless they are also static!

↙厌世 2024-08-28 03:06:13

顺便一提。该代码工作正常,没有任何 nullpointerexception
此代码打印 hi

我想知道当使用引用调用静态方法时内部会发生什么。

By the way. The code works fine without any nullpointerexception
This code prints hi

I wanted to know what happens internally when a reference is used to invoke a static method.

落花浅忆 2024-08-28 03:06:13

它之所以有效,是因为当使用引用调用静态方法时,该引用未使用。编译器查看调用该方法的表达式的声明/静态/编译时类型,并使用该类型来查找静态方法。

对变量调用静态方法不会带来任何好处,而且可能会让那些认为发生了多态调用的人感到困惑。

It works because when invoking a static method using a reference, the reference is not used. The compiler looks at the declared/static/compile-time type of the expression the method is being called on, and uses that type to find the static method.

You gain nothing by calling a static method on a variable, and you can confuse people who think a polymorphic call is occurring.

撩人痒 2024-08-28 03:06:13

调用Test.test()。由于 main 方法是静态并且位于同一个类中,因此您也可以直接调用 test()

Call Test.test(). As the main method is static and in the same class so you can also directly call test() too.

左耳近心 2024-08-28 03:06:13
class Test { 
    public static void test() { 
        System.out.println("hi"); 
    } 

    public static void main(String[] args) { 
        Test.test();
    }
}
class Test { 
    public static void test() { 
        System.out.println("hi"); 
    } 

    public static void main(String[] args) { 
        Test.test();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文