Java 为什么在显式调用构造函数时不能引用实例方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3685506/
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
Why can't I refer to an instance method while explicitly invoking a constructor?
提问by Koekiebox
Does anyone know why you can reference a static
method in the first line of the constructor using this()
or super()
, but not a non-static method?
有谁知道为什么您可以static
使用this()
or引用构造函数第一行中的方法super()
,而不是非静态方法?
Consider the following working:
考虑以下工作:
public class TestWorking{
private A a = null;
public TestWorking(A aParam){
this.a = aParam;
}
public TestWorking(B bParam)
{
this(TestWorking.getAFromB(bParam));
}
//It works because its marked static.
private static A getAFromB(B param){
A a = new A();
a.setName(param.getName());
return a;
}
}
And the following Non-Working example:
以及以下非工作示例:
public class TestNotWorking{
private A a = null;
public TestNotWorking(A aParam){
this.a = aParam;
}
public TestNotWorking(B bParam)
{
this(this.getAFromB(bParam));
}
//This does not work. WHY???
private A getAFromB(B param){
A a = new A();
a.setName(param.getName());
return a;
}
}
采纳答案by amorfis
Non-static methods are instance methods. This are only accessible in existing instance, and instance does not exist yet when you are in constructor (it is still under construction).
非静态方法是实例方法。这只能在现有实例中访问,并且当您在构造函数中时实例尚不存在(它仍在构建中)。
Why it is so? Because instance methods can access instance (non-static) fields, which can have different values in different instances, so it doesn't make sense to call such method on something else than existing, finished instance.
为什么会这样?因为实例方法可以访问实例(非静态)字段,这些字段在不同的实例中可以具有不同的值,所以在现有的、已完成的实例之外的其他东西上调用这样的方法是没有意义的。
回答by GHad
I think its's because final instance variables are not set yet (so you have no instance yet) and an instance method could access one. Whereas all static initialization has been done before the constructor call.
我认为这是因为尚未设置最终实例变量(因此您还没有实例)并且实例方法可以访问一个。而所有静态初始化都在构造函数调用之前完成。
Greetz, GHad
格雷茨,GHa
回答by mhshams
because when you calling this or super in constructor your object is not constructed yet. (your instance is not initialized completely yet). so calling an instance method doesn't make scene.
因为当您在构造函数中调用 this 或 super 时,您的对象尚未构造。(您的实例尚未完全初始化)。所以调用实例方法不会产生场景。
回答by Florin
If a method is non-static then you must call it on an object.
如果一个方法是非静态的,那么你必须在一个对象上调用它。
In the second example you would need to create an object of class TestNotWorking
and call getAFromB
on that object.
在第二个示例中,您需要创建一个类对象TestNotWorking
并调用getAFromB
该对象。
Something like:
就像是:
object = new TestNotWorking();
object.getAFromB(bParam);
回答by Thomas Mueller
TestNotWorking is not initialized at that point. The problem is: the first constructor (TestNotWorking(A aParam)) might call super() (internally it always does), meaning you would call a method before the constructor of the superclass is invoked. That's illegal.
此时 TestNotWorking 未初始化。问题是:第一个构造函数 (TestNotWorking(A aParam)) 可能会调用 super()(在内部它总是这样做),这意味着您将在调用超类的构造函数之前调用一个方法。那是非法的。
回答by Marc
See the Java Language Specification 8.8.7.1. This states that
请参阅Java 语言规范 8.8.7.1。这表明
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use
this
orsuper
in any expression; otherwise, a compile-time error occurs.
构造函数体中的显式构造函数调用语句不得引用在该类或任何超类中声明的任何实例变量或实例方法或内部类,或使用
this
或super
在任何表达式中;否则,会发生编译时错误。
This is because you can not call an instance method before the instance is created. By the way, it is possible to call an instance method later on in the constructor (although not a solution for you).
这是因为您不能在创建实例之前调用实例方法。顺便说一句,稍后可以在构造函数中调用实例方法(尽管这不是您的解决方案)。