<< Chapter < Page | Chapter >> Page > |
I'm not going to talk very much about instance methods and class methods in this module. However, there are a couple of characteristics of class methodsthat deserve a brief discussion in this context.
Cannot access instance members
First, the code in a class method has direct access only to other static members of the class. (A class method does not have direct access to instance variables or instance methods of the class.) This is sort of like saying that a class method has access to the methods and variablesbelonging to the Class object, but does not have access to the methods and variables belonging to the ordinary objects instantiated from the class described by the Class object.
Once again, be careful
Once again, this thinking breaks down very quickly once you get beyond static members. A Class object also has instance methods, such as getName , which can only be accessed using an actual reference to the Class object.
Now you are probably beginning to understand why I deferred this discussion until after I finished discussing the easy stuff.
No object required
Another important characteristic is that a class method can be accessed without a requirement for an object of the class to exist.
As with class variables, class methods can be accessed by joining the name of the class to the name of the method with a period.
I will illustrate much of this with a sample program named MyClass01 .
Discuss in fragments
I will discuss the program in fragments. You will find a complete listing of the program in Listing 13 near the end of the module.
Listing 1 shows the beginning of the class definition.
Listing 1 . Beginning of the class named MyClass01. |
---|
class MyClass01{
static Date v1 = new Date();Date v2 = new Date(); |
Two member variables
The code in Listing 1 declares two member variables, named v1 and v2 , and initializes each of those variables with a reference to a new object of the Date class. (When instantiated using the constructor with no arguments, the new Date object encapsulates the current date and time from the system clock.)
Note the static keyword
The important thing to note here is the use of the static keyword when declaring the variable named v1 . This causes v1 to be a class variable , exhibiting the characteristics of class variables described earlier.
An instance variable
On the other hand, the variable named v2 is not declared static . This causes it to be an instance variable , as described above.
The main method is a class method
Listing 2 shows the signature for the main method.
Listing 2 . Signature of the main method. |
---|
public static void main(String[] args){ |
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 called 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.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?