The important thing to note here is that the main method is declared static. That causes it to be a class method.
As a result, the main method can be invoked without a requirement for an object of the class to exist.
(Also, the main method has direct access only to other static members.)How a Java application starts running
In fact, that is how the Java Virtual Machine starts an application running.
First the JVM finds the specified file having an extension of .class. Then it examines that file to see if it has a main method with the correct signature. If not, an error occurs.
If the JVM finds a main method with the correct signature, it invokes that method without instantiating an object of the class. That is how the Java Virtual Machine causes a Java application to start running.
A side note regarding appletsA poor programming technique
For those of you who are familiar with Java applets, you should know that this is not the case for an applet. You should know that an applet does not use a main method. When an applet is started, an object of the controlling class is instantiated by the browser, by the appletviewer program, or by whatever program is being used to control the execution of the applet.
Basically, this entire sample program is coded inside the main method. As a practical manner, that is a very poor programming technique, but it works well for this example.
Display some text
The code in Listing 3, which is the first executable statement in the main method, causes the words Static variable to appear on the computer screen. I will come back and discuss the details of this and similar statements later in the lesson.
System.out.println(
"Static variable");
Listing 3Display date information
Proceeding down through the code in the main method, the code in Listing 4 causes the current contents of the Date object referred to by the contents of the class variable named v1 to be displayed on the computer screen.
System.out.println(MyClass01.v1); Listing 4
No object required
For the moment, concentrate on the boldface text in the statement in Listing 4.
IMPORTANT: Because the variable named v1 is a class variable, it's value is accessed by joining the name of the class to the name of the variable with a period.
What was the output?
I will also discuss the remaining portion of statements of this sort later. For now, just be aware that it caused the output shown in Figure 1 to be displayed on my computer screen when I ran the program.
Mon Sep 17 09:52:27 CDT 2001 Figure 1
Displays date and time
Obviously, the date and time displayed will depend on when you run the program.
Pay particular attention to the seconds portion of the time. I will refer back to this later.
A five-second delay
The code in Listing 5 (still in the main method) causes the main thread of the program to go to sleep for five seconds. Don't worry about it if you don't understand this code. The only reason that I included it in the program was to force a five-second delay in the execution of the program.
try{
Thread.currentThread().sleep(5000);
}catch(InterruptedException e){}
Listing 5Instantiate a new object
Having caused the program to sleep for five seconds, the code in Listing 6 instantiates a new object of the class named MyClass01. The code stores the new object's reference in the reference variable named ref1.
MyClass01 ref1 = new MyClass01(); Listing 6
A new Date object also
Recall from Listing 1 above that the class declares an instance variable named v2 of the type Date.
When the new object is instantiated by the code in Listing 6, a new Date object is also instantiated. A reference to that object is stored in the instance variable named v2.
(In other words, the new object of the class MyClass01 owns a reference to a new object of the class文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




