JAVATM- The Beginnings

Objects and References

Section 1 - Objects Defined

An object is an example of a class. The term instance is frequently used in place of example. When we create an object, it contains its own copy of all non-static fields. Class Box has non-static fields length, width, and height. Each Box object we create has a separate length, width, and height. The length, width, and height of one Box object can be different from those of another Box object.


Section 2 - Object Declarations

Objects need to be declared just as do variables of primitive types. Object declarations have the following form:

ClassName objectName = new ClassName();

The declaration begins with the name of the class. This is followed by the name of the object, which can be any valid Java identifier that is not a keyword, and is not previously used. This is followed by an equals sign and the keyword new. The name of the class is listed again after new, followed by parentheses and ended with a semicolon.

The following is a declaration which creates a Box object:

        Box box1 = new Box();

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.

Back to