views:

74

answers:

6

In this code I see the following lines before the "main" method:

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. All other methods will be executed if they are called from the "main" method. 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?

+2  A: 

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.

Jonathan Feinberg
A: 

You haven't included the whole file.

These are the declarations of fields. It indicates that when the class is instantiated (i.e., 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. 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. 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.

Uri
A: 

it specifies the types.

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

These are members (reference) variables of the KeyEventDemo class.

When KeyEventDemo is instantiated using the new keyword, each instance will have a these variables to refer to a JTextArea and JTextField respectively. These are initialized to null and are assigned as references to a couple of instances in the addComponentsToPane method.

McDowell
A: 

Those declarations are evaluated when the object is instantiated by the java virtual machine. That is right before the main methode is this case.

DerMike
The second sentence is incorrect. The `main` method is `static` and (in the linked code) is called *before* the class is instantiated. (The `main` method then calls another static method which creates an instance of the class.)
Stephen C
Steven is right. Thank you.
DerMike
A: 

Those are class members.

Basically, in a java class, you have methods and members. 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. 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.

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.

Chad Okere
I think calling them "class members" is misleading as they are not marked static, calling them instance variables is appropriate.
sateesh