Java Reference
In-Depth Information
Java syntax: new-expression
class-name ( arguments )
Example : new JFrame( " title 1 " )
Purpose : Create a new folder of class class-name , ini-
tialize its fields using the call class-name ( argu-
ments ) , and yield the name of the new folder as
the value of the new-expression.
Objects of class JFrame
Class javax.swing.JFrame is a good class to study first because you will be
able to use it and see immediately the effects of calling methods of one of its
manila folders. An instance of JFrame is associated with a window on your com-
puter monitor. To create an object of class JFrame , use this assignment:
JFrame window= new JFrame("first JFrame");
Figure. 1.4 shows three things. On the left is the filing cabinet with a drawer for
class JFrame . To the right is the object, drawn like a manila folder, that is creat-
ed by execution of the above assignment; this object belongs in JFrame 's file
drawer. The name on the tab of the folder, a3 , is arbitrary but different from the
names of all other folders. Finally, Fig. 1.4 contains a variable window , with the
name a3 as its value. We describe the process that created the picture.
First, the new-expression is evaluated:
new JFrame("first JFrame")
Evaluating this new-expression is a three-step process:
1. Create a new folder of class JFrame and give it a name.
2. Execute the call JFrame("first JFrame") , which causes the argument
to be stored somewhere in the folder. This String will be the title on the
3. Yield the name of the folder as the value of the new-expression.
Take a look near the top left of the folder in Fig. 1.4. It contains “ JFrame
(String) ”, which indicates that method JFrame is in the folder and, when this
method is called, it has to have an argument that is a String . This is the method
that is called in step 2 above.
Second, once the new-expression yields its value, a3 , the assignment state-
ment stores that value in variable window .
The contents of folder a3
We investigate the contents of folder a3 in Fig. 1.4. It has method
JFrame(String) , as discussed above, several other instance methods, and an
instance variable title .
Search WWH ::

Custom Search