【发布时间】:2014-07-09 11:23:55
【问题描述】:
似乎是一个非常基本的查询,但我在思考下面的静态方法 main() 如何能够使用 new 关键字从中执行非静态方法(显然是构造函数)。虽然我知道 new 还带来了一些其他的东西,但我应该如何说服自己这不是静态和非静态方法不能使用非静态规则的例外和静态上下文?
下面是示例代码:
public class ConstructorTest {
ConstructorTest(String str)
{
System.out.println("Constructor Printing "+str);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConstructorTest cnst=new ConstructorTest("here");
}
}
上面的代码实际打印了 --> 构造函数在这里打印
或换句话说,从静态方法执行非静态方法的主体?
欢迎任何合理的解释。
【问题讨论】:
标签: java constructor static non-static