简体   繁体   English

在main方法之前做什么“ ClassName objectName;”?

[英]What “ClassName objectName;” does before the main method?

In this code I see the following lines before the "main" method: 这段代码中,我在“ main”方法之前看到以下几行:

JTextArea displayArea;
JTextField typingArea;

I wonder what these lines do and when they are executed. 我想知道这些行的作用以及何时执行。 As far as I know the "main" method is the "entry point". 据我所知,“主要”方法就是“入口点”。 So, the code will be executed from the beginning of the "main" method. 因此,将从“ main”方法的开头执行代码。 All other methods will be executed if they are called from the "main" method. 如果从“ main”方法调用所有其他方法,则将执行它们。 If it is so, the mentioned 2 lines will never be executed. 如果是这样,将永远不会执行上述两行。 Moreover, even if they will be executed, what exactly they do? 而且,即使它们将被执行,它们究竟会做什么? What do these pairs of "ClassName objectName" do? 这些“ ClassName objectName”对是做什么的?

Those are called "declarations". 这些被称为“声明”。 They are declaring the existence of two variables, stating their types and their names . 他们宣布存在两个变量,说明它们的类型名称 The location of the declaration determines their scope , in other words, which parts of the program are allowed to know about those particular variables, and may refer to them. 声明的位置决定了它们的范围 ,换言之,允许程序的哪些部分了解这些特定变量,并且可以引用它们。

Here's a tutorial about Java variables . 这是有关Java变量的教程

You haven't included the whole file. 您尚未包括整个文件。

These are the declarations of fields. 这些是字段的声明。 It indicates that when the class is instantiated (ie, an object is created from it), each object will have a reference to a text area and a text field. 它指示在实例化该类时(即,从该类创建一个对象),每个对象将具有对文本区域和文本字段的引用。 You will, however, have to create these objects. 但是,您将必须创建这些对象。

Notice how your main method is static. 注意您的主要方法是静态的。 This means that it can run without the containing class being instantiated. 这意味着它可以在不实例化包含类的情况下运行。 However, most Java methods operate on objects. 但是,大多数Java方法对对象进行操作。 Suppose that your main is in class C. It is likely that somewhere in your main, you will see a "new C" which means that an instance of C is created. 假设您的main在C类中。您的main中的某个地方很可能会看到“新C”,这表示已创建C的实例。 Then some other operation will be invoked on that new object (look for other, non-static methods in the file), and these operations will manipulate these two fields. 然后,将对该新对象调用其他一些操作(在文件中查找其他非静态方法),这些操作将操纵这两个字段。

it specifies the types. 它指定类型。

JTextArea displayArea; // this creates textarea type and thus textbox
JTextField typingArea; // thus creates textfield type var and thus text field

These are members (reference) variables of the KeyEventDemo class. 这些是KeyEventDemo类的成员(引用)变量。

When KeyEventDemo is instantiated using the new keyword, each instance will have a these variables to refer to a JTextArea and JTextField respectively. 当使用new关键字实例化KeyEventDemo ,每个实例将具有一个这些变量,分别引用JTextAreaJTextField These are initialized to null and are assigned as references to a couple of instances in the addComponentsToPane method. 它们被初始化为null ,并被分配为对addComponentsToPane方法中几个实例的引用。

Those declarations are evaluated when the object is instantiated by the java virtual machine. 当Java虚拟机实例化对象时,将评估这些声明。 That is right before the main methode is this case. 在这种情况下,就在main方法之前。

Those are class members. 这些是班级成员。

Basically, in a java class, you have methods and members. 基本上,在Java类中,您具有方法和成员。 The members are variables that hold the state of Objects that are instances of that class. 成员是变量,用于保存作为该类实例的对象的状态。

The main method is separate, it's a static method so it can run without there being an instance of the class. 主要方法是单独的,它是一个静态方法,因此可以在没有类实例的情况下运行。 So you have logic that runs in main() and you have logic that runs on an instance of the class. 因此,您具有在main()中运行的逻辑,并且您具有在类的实例上运行的逻辑。 They're quite separate. 他们是完全分开的。

If you want too, in your main() function you can create an instance of the class, and then that could would 'run' if there is any initialization of the member functions that needs to be done. 如果需要的话,可以在main()函数中创建该类的实例,然后在需要对成员函数进行任何初始化的情况下将其“运行”。

However, not all class members are initialized in the constructor, and those would stay null (so in that case, nothing would 'execute' at that point). 但是,并不是所有的类成员都在构造函数中初始化,而这些成员将保持为空(因此,在这种情况下,任何东西都不会“执行”)。 In your example, there's no initialization, so those members would be null when the constructor logic starts. 在您的示例中,没有初始化,因此在构造函数逻辑启动时那些成员将为null。

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

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