<< Chapter < Page Chapter >> Page >

If the JVM finds a main method with the correct signature, it calls that method without instantiating an object of the class. That is howthe Java Virtual Machine causes a Java application to start running.

A side note regarding applets

For those of you who are familiar with Java applets, you should know that this is not the case for an applet. 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 theapplet.

A poor programming technique

Basically, this entire sample program is coded inside the main method. As a practical manner, this is a very poor programming technique, but itworks 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 thisand similar statements later in the module.

Listing 3 . Display some text.
System.out.println("Static variable");

Display date information

Continuing with 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.

Listing 4 . Display date information.
System.out.println(MyClass01.v1);

No object required

For the moment, concentrate on the text inside the parentheses in the statement in Listing 4 .

Because the variable named v1 is a class variable, it's value is accessed by joining the name of the class to thename of the variable with a period.

What was the output?

I will discuss the remaining portion of statements of this sort later. For now, just be aware that the code in Listing 4 caused the output shown in Figure 1 to be displayed on my computer screen when I ran the program.

Figure 1 . Output date and time.
Mon Sep 17 09:52:27 CDT 2001

Displays date and time

Obviously, the date and time displayed will depend on when you run the program. As you can see, I first wrote this module and ran this program in 2001.

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'tworry 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 ofthe program.

Listing 5 . A five-second delay.
try{ Thread.currentThread().sleep(5000);}catch(InterruptedException e){}

Instantiate 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 . This code stores the new object's reference in the reference variable named ref1 .

Listing 6 . Instantiate a new object .
MyClass01 ref1 = new MyClass01();

A new Date object also

Recall from Listing 1 above that the class declares an instance variable named v2 of the type Date .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask