<< Chapter < Page | Chapter >> Page > |
Create an instance of Worker
The main method uses the new operator along with the default constructor for the class named Worker to create a new instance of the class named Worker (an object of the Worker class) . This is often referred to as instantiating an object.
A reference to an anonymous object
The combination of the new operator and the default constructor for the Worker class returns a reference to the new object. In this case, the object is instantiated as an anonymous object , meaning that the object's reference is not saved in a named reference variable. (Instantiation of a non-anonymous object will be illustrated later.)
Call hello method on Worker object
The main method contains a single executable statement.
As soon as the reference to the new object is returned, the single statement in the main method calls the hello method on that reference.
Output to standard output device
This causes the hello method belonging to the new object (of the class named Worker ) to execute. The code in the hello method calls the println method on the static variable of the System class named out .
Lots of OOP embodied in the hello method
I often tell my students that I can tell a lot about whether a student really understands object-oriented programming in Java by asking them to explaineverything that they know about the following statement:
System.out.println("Hello World");
I would expect the answer to consume about ten to fifteen minutes if the student really understands Java OOP.
The one-minute version
When the virtual machine starts a Java application running, it automatically instantiates an I/O stream object linked to the standard output device (normally the screen) and stores a reference to that object in the static variable named out belonging to the class named System .
Call the println instance method on out
Calling the println method on that reference, and passing a literal string ("Hello World") to that method causes the contents of the literal String object to be displayed on the standard output device.
Display Hello World on the screen
In this case, this causes the words Hello World to be displayed on the standard output device. This is the answer to the original question.
Time for main method to terminate
When the hello method returns, the main method has nothing further to do, so it terminates. When the main method terminates in a Java application, the application terminates and returns control to the operating system.This causes the system prompt to reappear.
A less-cryptic form
A less cryptic form of this program is shown in Listing 17 .
Listing 17 . Answer 1. |
---|
public class Ap002{
public static void main(String args[]){Worker refVar = new Worker();
refVar.hello();}//end main()
}//end class definitionclass Worker{
public void hello(){System.out.println("Hello World");
}//end hello()}//end class definition |
Decompose single statement into two statements
In this version, the single statement in the earlier version of the main method is replaced by two statements.
A non-anonymous object
In the class named Ap002 shown in Listing 2 , the object of the class named Worker is not instantiated anonymously. Rather, a new object of the Worker class is instantiated and the object's reference is stored in (assigned to) the named reference variable named refVar .
Call hello method on named reference
Then the hello method is called on that reference in a separate statement.
Produces the same result as before
The final result is exactly the same as before. The only difference is that a little more typing is required to create the source code for the secondversion.
Will often use anonymous objects
In order to minimize the amount of typing required, I will probably use the anonymous form of instantiation whenever appropriate in these modules.
Now that you understand the framework ...
Now that you understand the framework for the program code, I can present more specific questions. Also, the explanations will usually be shorter.
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?