Objects - A First Look

Objects and their Construction

We will go into much greater detail regarding exactly what a Java "object" is later, but for now -- you can think of an object as something that can both store data and perform various actions.

The data might include information stored in various primitive data types or even other objects.

The actions an object can take are described by the methods associated with that object.

Exactly what type of data is stored and the details of the methods it possesses are defined by the object's class. For now, you can think of a class as a special "type", created by a programmer -- and a blueprint for the construction of corresponding objects.

For example, there is a pre-defined class called JButton that can be used to create buttons like the ones you might see in an application window.

An instance of the JButton class is a single JButton object. JButton objects store data in that they have a height, a width, a position, text on the button, etc... JButton objects have methods that generally take some action, when they are clicked. They may also do something when you hover over them with the mouse (like light up).

A JButton object (and every object, for that matter) must be stored in memory -- and like their primitive-data-type cousins, referencing these objects and the data they contain can be accomplished through the use of variables.

Although not surprisingly given their potential complexity, initialization and assignment for objects works a bit differently than initialization and assignment for primitive data types. For example, suppose we wish to have a variable called myButton refer to a JButton object. Knowing that there is a lot of data associated with a single button. (height, width, position, text on the button, etc..., as just mentioned), what would you put after the equals sign in the code below?

JButton myButton = … ;

There are many assignments that need to be made here -- many actions to be taken. Of course, performing some action (or actions) is what methods were designed to do. In every class (that you can instantiate with an object), there will be a special method called the constructor that does all of the things that need to be done to create a new object of that class.

This constructor method always has the same name as the class, followed (as always) by some parentheses which may or may not include some additional parameters the method might need.

To create the object (that your variable will then reference) you need to use the “new” keyword followed by the call to the constructor method.

So, for example, to create two new JButton objects named myButton1 and myButton2, we would write the following:

JButton myButton1 = new JButton();      \\new JButton with no text
JButton myButton2 = new JButton("OK");  \\new JButton with text "OK"

As with primitive types, you can split the declaration and instantiation/initialization up into two steps:

JButton myButton;
myButton = new JButton();