The main method can also access other classes along with the variables and methods of those classes and of objects instantiated from those classes.
Our hello1 class is repeated below for convenience.
/*File hello1.java Copyright 1997, R.G.Baldwin
**********************************************************/
class hello1 { //define the controlling class
//define main method
public static void main(String[] args){
//display text string
System.out.println("Hello World");
}//end main
}//End hello1 class. No semicolon at end of Java class.
|
The out variable referred to above as System.out is a class variable of the System class.
Remember, a class variable can be accessed without the requirement to instantiate an object of the class. Remember also that the class variable (as is the case with all variables) must be of some specific type.
A class variable may be of a primitive type, or it may be a reference variable which refers to another object.
As you will see, the variable named out in this case is a reference variable which refers to an object of another type.
You access class variables or class methods in Java by
joining
the name of the class to the name of the variable or method with a period.
| In Java, System.out accesses the class variable named out in the Java class named System. |
| C++ programmers will recall that the scope resolution operator (::) is used to access class variables and class methods in C++. |
The out variable in the System class refers to (points
to) an instance of the PrintStream class (a PrintStream object)
which is automatically instantiated when the System class
is loaded into the application.
| This is somewhat analogous to the fact that the input and output stream objects, cin and cout, are automatically instantiated and linked to the standard input and standard output devices when a C++ program begins execution. |
The PrintStream class has an instance method named println()
which causes its argument to be displayed on the standard output device.
| Hopefully you already know about the standard input device and
standard
output device. If not, you need to go back and learn about them.
In brief, The standard output device is normally the screen on the operator's console by default, but can be redirected to another device by the user at the operating system level. |
Thus, assuming that the standard output device has not been redirected,
the following statement causes the string Hello World to be displayed
on the screen.
System.out.println("Hello World");
|
As I indicated when I started this lesson, this is our first introduction to the complexity which can result from use of the OOP paradigm. In gets worse in some cases. If this is not clear to you, you need to go back over it and think about it until it becomes clear.
Class variables are themselves either primitive variables or references to objects (instances of some class). The referenced object may provide methods to control the behavior of the object. In the above case, we accessed the println method of the single instance of an object of the PrintStream class referred to by the class variable named out.
As a side note, in addition to class variables, both Java and C++ provide instance variables and instance methods. Every instance of a class has its own set of instance variables. You can only access instance variables and instance methods through an object of the class.
A - True
Q - The main method cannot access the variables and methods of objects instantiated from other classes: True or False?
A - False. The main method can access the variables and methods of objects instantiated from other classes. Otherwise, the flow of the program would be stuck within the main method itself and wouldn't be very useful.
Q - The main method must instantiate objects of other classes in order for the program to execute: True or False?
A - While it is probably true that the main method must instantiate objects of other classes in order to accomplish much that is of value, this is not a requirement. The main method in the "Hello World" program of this lesson does not instantiate objects of any class at all.
Q - In order to be useful, the System class must be used to instantiate objects in a Java application: True or False?
A - False. The System class has several class variables (including out and in) that are useful without the requirement to instantiate an object of the System class.
Q - Class variables such as the out variable of the System class must be of some specific type: True or False?
A - True
Q - Class variables must be of a primitive type such as int or float: True or False?
A - False. A class variable can be a primitive type, or it can be a reference variable which points to another object.
Q - The out variable in the System class is of a primitive type: True or False?
A - False. the variable out defined in the System class is a reference variable which points to an object of another type.
Q - What does the code fragment System.out access?
A - The code fragment System.out accesses the class variable named out in the class named System.
Q - An object of type PrintStream is automatically instantiated when the System class is loaded into an application: True or False?
A - True
Q - The out variable in the System class refers to an instance of what class?
A - The out variable in the System class refers to an instance of the PrintStream class (a PrintStream object) which is automatically instantiated when the System class is loaded into the application.
Q - The println method is an instance method of what class?
A - The println method is an instance method of the PrintStream class.
Q - What is the primary behavior of the println method?
A - The println method causes its argument to be displayed on the standard output device. (The standard output device is the screen by default, but can be redirected by the user at the operating system level.)
Q - How can the println method be accessed?
A - The println method can be accessed by joining the name of a variable that references a PrintStream object to the name of the println method using a period.
Q - Assuming that the standard output device has not been redirected, write a code fragment that will cause your name to be displayed on the screen.
A - System.out.println("Java programming");
Q - Explain how your code fragment produces the desired result.
A - The above statement invokes the println method of an object of the PrintStream class which is referenced (pointed to) by the out variable which is a class variable of the System class.
Q - If you have a class named MyClass that has a class variable named myClassVariable which requires four bytes of memory and you instantiate ten objects of type MyClass, how much total memory will be allocated to contain the allocated variables (assume that the class definition contains no other class, instance, or local variables).
A - The runtime system allocates a class variable only once no matter how many instances of the class are instantiated. Thus, all objects of the class share the same physical memory space for the class variable, and in this case, only four bytes of memory will be allocated to contain the allocated variables.
Q - In the code fragment
System.out.println("Java programming");
how many actual instances of the variable named out are allocated in memory?
A - Only one, because out is a class variable of the System class.
Q - If you have a class named MyClass that has an instance variable named myInstanceVariable which requires four bytes of memory and you instantiate ten objects of type MyClass, how much total memory will be allocated to contain the allocated variables (assume that the class definition contains no other class, instance, or local variables).
A - Every instance of a class has its own set of instance variables. You can only access instance variables and instance methods through an object of the class. In this case, forty bytes of memory would be required to contain the instance variables of the ten objects.