Creating an Instance of a Class

   

To use a class you have defined, you need to create an instance of that class. An instance is an object whose data type is the class. Any class you create can be instantiated , just like any other reference data type in Java.

The new Operator

Declaring a reference to an object in Java does not actually create an object.

The following declaration creates a reference to a Circle but does not create a Circle:

 Circle myCircle; 

Following this declaration, myCircle is a null reference if it's a field, or it is undefined if it's a method variable. Objects are created using the new operator as shown here:

 Circle myCircle = new Circle(); 

The new operator is followed by the signature of the constructor to call for initialization of the object. In the preceding example, the created Circle instance is constructed using the no-argument constructor for the class. The new operator returns a reference to the newly created Circle that is assigned to the myCircle variable. A compiler error occurs if there is no constructor corresponding to the signature used with the new operator. As you might have noticed, the primary difference between declaring a reference type, such as Circle, and a primitive type, such as int, is the use of new.

You might be wondering how a Java application or applet starts running when you don't have any way to actually instantiate a class to start the execution. The answer lies with the virtual machine.

When the Java program is run to start an application or a browser encounters an <APPLET> tag, the virtual machine does a few things for you. In the case of an application, when you type java MyClass, the virtual machine calls the static main() method in MyClass. This does not create an instance of MyClass, but, because main() is a static method, an instance is not necessary. As pointed out previously, you will typically create an instance of your class in the main() method. In the case of an applet, the browser creates an instance of MyClass when it encounters <APPLET CODE="MyClass"> and automatically calls the init() method.

Garbage Collection

Although Java uses the C++ concept of a constructor, it does not support the complementary destructor method. A major advantage of Java is that you, as a programmer, are not directly responsible for freeing the memory that has been dynamically allocated using the new operator. Hence, Java does not include a delete operator. This automatic reclaiming of memory provided by Java is referred to as garbage collection.

An object is available for garbage collection when there are no longer any references to that object. Garbage collection occurs at intervals determined by the virtual machine, so objects that are available for collection are not immediately reclaimed. You can call System.gc() to request that garbage collection take place but this is merely a suggestion to the virtual machine that it is not obligated to obey.

Caution

A common problem in Java programs is the existence of extraneous references to objects that are no longer needed. These references prevent the associated objects from being reclaimed and result in memory leaks during execution. You need to pay careful attention to setting references to unneeded objects to null, especially in memory- intensive programs.


   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net