The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
Listing 2The 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标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
