java 静态方法访问非静态构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10513633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static method access to non-static constructor?
提问by Ahmet Yildirim
I had an exam yesterday on Java. There is something which seem really ambiguous to me.
昨天我参加了 Java 考试。有一些事情对我来说似乎非常模棱两可。
Rules are simple:
规则很简单:
- Static method cannot cannot call non-static methods.
Constructors are kind of a methodwith no return type.
public class Main { public static void main(String[] args) { Main p = new Main(); k(); } protected Main() { System.out.print("1234"); } protected void k() { } }
Main p = new Main() line prints 1234 k() line raises error
- 静态方法不能调用非静态方法。
构造函数是一种没有返回类型的方法。
public class Main { public static void main(String[] args) { Main p = new Main(); k(); } protected Main() { System.out.print("1234"); } protected void k() { } }
Main p = new Main() line prints 1234 k() line raises error
So why did this happen? Doesn't it conflict with the rules of Java above?
那么为什么会发生这种情况呢?是不是和上面Java的规则冲突了?
回答by aioobe
1 - Static method cannot cannot call non-static methods.
1 - 静态方法不能调用非静态方法。
Sure they can, but they need an object to call the method on.
当然可以,但是他们需要一个对象来调用 上的方法。
In a static method, there's no this
reference available, so foo()
(which is equivalent to this.foo()
) is illegal.
在静态方法中,没有this
可用的引用,因此foo()
(相当于this.foo()
)是非法的。
2 - Constructors are kind of a method with no return type.
2 - 构造函数是一种没有返回类型的方法。
If they should be compared to methods, I would say constructors are closer to non-static methods(since there is indeed a this
reference inside a constructor).
如果将它们与 methods进行比较,我会说构造函数更接近于非静态方法(因为this
构造函数内部确实存在引用)。
Given this view, it should be clear to you why a static method can call a constructor without any problems.
鉴于这种观点,您应该清楚为什么静态方法可以毫无问题地调用构造函数。
So, to sum it up:
所以,总结一下:
Main p = new Main();
is okay, since new Main()
does not rely on any existing object.
没关系,因为new Main()
不依赖任何现有的对象。
k();
is not okay since it is equivalent to this.k()
and this
is not available in your (static) main method.
不好,因为它等效于this.k()
并且this
在您的(静态)main 方法中不可用。
回答by Péter T?r?k
No. Constructors aren't ordinary methods in this respect. The whole point of the constructor is to, well, construct a new instance of the class.
不。在这方面,构造函数不是普通的方法。构造函数的全部意义在于,构造类的一个新实例。
So it can be invoked in static scope too. Just think about it: if you needed an existing instance of your class in order to create a new instance of it, you would simply never be able to instantiate it ever.
所以它也可以在静态范围内调用。想想看:如果您需要类的现有实例来创建它的新实例,那么您将永远无法实例化它。
A few clarifications:
几点说明:
Static method cannot cannot call non-static methods.
静态方法不能调用非静态方法。
Not quite. You can call a nonstatic method from inside a static method, just you need to scopeit to a specific object of that class. I.e.
不完全的。您可以从静态方法内部调用非静态方法,只需要将其范围限定为该类的特定对象。IE
p.k();
would work perfectly in your code sample above.
将在您上面的代码示例中完美运行。
The call
电话
k();
would be fine inside an instance (nonstatic) method. And it would be equivalent to
在实例(非静态)方法中会很好。它相当于
this.k();
The implied this
refers to the current instance of the class. Whenever the compiler sees an unqualified call like k()
within an instance method, it will automatically scope it with this.
. However, since static methods aren't tied to any instance of the class, you (and the compiler) can't refer to this
inside a static method. Hence you need to explicitly name an instance of the class to call an instance method on.
隐含this
指的是类的当前实例。每当编译器k()
在实例方法中看到不合格的调用时,它就会自动使用this.
. 但是,由于静态方法不绑定到类的任何实例,您(和编译器)不能this
在静态方法内部引用。因此,您需要显式命名类的实例以调用实例方法。
回答by Jon Skeet
Rules are simple:
1 - Static method cannot cannot call non-static methods.
规则很简单:
1 - 静态方法不能调用非静态方法。
That's simply not true. A static method cancall a non-static method, just via a "target" reference. For example, this is fine in a static method:
这根本不是真的。静态方法可以调用非静态方法,只需通过“目标”引用即可。例如,这在静态方法中很好:
Integer x = Integer.valueOf(10);
int y = x.intValue(); // Instance method!
The realpoint is "there's no this
reference within a static method".
在真正的问题是“有没有this
一个静态方法中引用”。
2 - Constructors are kind of a method with no return type.
2 - 构造函数是一种没有返回类型的方法。
That's not a really useful model, to be honest. It makes moresense (from the caller's point of view) to consider a constructor as a static method with a return type that's the same as the declaring class, but even that's not a perfect model by any means.
老实说,这不是一个真正有用的模型。它使更多的意义(从查看来电者的角度)考虑构造与返回类型这是一样的声明类的静态方法,但即使这样的不以任何方式一个完美的模型。
I suggest you think of a constructor as a different type of member. Embrace the differences between constructors and methods, instead of trying to hide them.
我建议您将构造函数视为不同类型的成员。接受构造函数和方法之间的差异,而不是试图隐藏它们。