A class in Java can be very simple. Here is a class definition for an empty class:
class MyClass { }
Obviously, this class is not yet useful, but it is legal in Java. A slightly better class would contain some data members and methods, which will be added shortly. First, however, the syntax for instantiating a class must be covered. To create an instance of this class, the new operator is used in conjunction with the class name. An instance variable must be declared for the object.
MyClass myObject;
This, however, does not allocate memory and other resources for the object. This creates a reference called, myObject, but does not instantiate the object. The new operator performs this task.
myObject = new MyClass();
Notice that the name of the class is used as if it were a method. This is not coincidental (as you will see in an upcoming section). Once this line of code has executed, the member variables and methods of the class (which do not yet exist) can be accessed using the "." operator.
Once you have created the object, you never have to worry about destroying it. Objects in Java are automatically garbage collected. As soon as the object reference (i.e., the variable) goes out of scope, the virtual machine will automatically deallocate any resources allocated by the new operator.