简体   繁体   English

Java:何时初始化变量“ this”?

[英]Java: When is the variable “this” initialized?

public class MainMDI extends javax.swing.JFrame {

   private static MainMDI thiz; 

      public MainMDI() {
        initComponents();
        thiz = this;
      }
}  

I'm creating an MDI application in swing. 我正在创建一个MDI应用程序。 Class MainMDI is the main class of the application and therefore the main method resides in that class. MainMDI类是应用程序的主要类,因此main方法位于该类中。 The above code creates a static variable called thiz that points to the instance of class MainMDI when the application runs. 上面的代码创建了一个名为thiz的静态变量,该静态变量在应用程序运行时指向类MainMDI的实例。

I'm planning to use variable thiz to access non-static (instance) members of class MainMDI from within the main method.(I cannot access non-static members from from within the main method since the main method is a static member in class MainMDI in my application). 我打算使用变量thiz从main方法内部访问MainMDI类的非静态(实例)成员。(我无法从main方法内部访问非静态成员,因为main方法是类中的静态成员。我的应用程序中的MainMDI)。

public class MainMDI extends javax.swing.JFrame {

   private static MainMDI thiz = this; 

      public MainMDI() {
        initComponents();
      }
}  

But when I attempt to initialize variable thiz as in the above code, compiler says non-static variable this cannot be referenced from a static context. 但是,当我尝试像上面的代码中那样初始化变量thiz时,编译器说非静态变量不能从静态上下文中引用。 But I'm not referring to this in a static context here am I? 但是我不是在静态环境中指这吗? Is this because variable this, being non-static, is not yet initialized when the static variable this is initialized? 这是因为静态变量this初始化时尚未初始化的非静态变量吗?

Also, would it have been a better programming practice if I had not set class MainMDI as the main class and created another class with a main method in it and set that class as the main class? 另外,如果我没有将MainMDI类设置为主类,而是创建了另一个具有main方法的类并将该类设置为主类,那将是更好的编程实践吗?

But when I attempt to initialize variable thiz as in the above code, compiler says non-static variable this cannot be referenced from a static context. 但是,当我尝试像上面的代码中那样初始化变量thiz时,编译器说非静态变量不能从静态上下文中引用。 But I'm not referring to this in a static context here am I? 但是我不是在静态环境中指这吗?

Yes, you are. 是的,你是。 Static class variables are initialized when the class is loaded (not when an object instance is being created). 静态类变量在加载类时初始化(而不是在创建对象实例时)。 There is no this in that context. 在这种情况下没有this The code: 编码:

private static javax.swing.JFrame thiz = this; 

Will simply not work. 根本行不通。 Despite your assertions to the contrary, you do want a singleton. 尽管您的主张相反,但您确实想要单身人士。 Otherwise, given N possible object instances of your MainMDI object, which one would you be expecting to access from a static context? 否则,给定MainMDI对象的N个可能的对象实例,您期望从静态上下文访问哪个实例? You should consider refactoring your code rather than trying to strong-arm around Java language semantics. 您应该考虑重构代码,而不是尝试围绕Java语言语义进行强力武装。

this means "the object instance currently being operated on", it only makes sense inside a non-static member function. this意味着“当前正在操作的对象实例”,仅在非静态成员函数内部有意义。 In general this is implicitly passes to each non-static member function when you call that member function so it'd be fair to say that it is initialized right before a non-static member function gets called. 总的来说this是隐含当你调用该方法,所以它会是公平地说,这是初始化的非静态成员函数被调用权利之前传递到每一个非静态成员函数。

Whether factoring out a class with "main" method is a good idea will heavily depend on actual implementation details. 用“ main”方法分解类是否是一个好主意,将在很大程度上取决于实际的实现细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM