Java Reference
In-Depth Information
When you execute the currently selected class, NetBeans compiles the code to a set
of binary files, and then transfers control to the main() method. That method, in turn,
does the following:
1.
Executes HelloMessage hm to create a variable named hm that is
capable of holding an instance of the class HelloMessage . The
variable hm is empty at this point.
2.
Invokes new HelloMessage() to create an object of the class by
that name. The no-arg constructor will be executed, and "Default
Message" is now the greeting text. The new object is now stored in
the variable hm .
3.
Makes a call to System.out.println() to show that the ob-
ject's no-arg constructor has indeed executed as expected. The greet-
ing "DEFAULT MESSAGE" is displayed in the “Output” pane.
4.
Sets the message to be the traditional text "Hello, World" .
5.
Makes another call to System.out.println() to output the
new message that has just been set. Now you see the greeting
"hello, world" added to the “Output” pane.
The pattern in the solution is common in Java programming. The main() method
is where execution begins. Variables are defined, and objects are created using the new
operator. Object variables are often set and retrieved using setter and getter methods.
Tip Command-line apps are passé. System administrators and programmers some-
times write them as utilities, or to batch-process large amounts of data. But in the main,
today's applications are GUI applications. JavaFX is the way forward in writing those
applications, and you can learn about it in Chapters 14 through 17 . Recipe 14-1 provides
what is essentially a “Hello, World” application in GUI form.
1-3. Compiling and Executing from the
Command-line Interpreter
Problem
Search WWH ::




Custom Search