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 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).
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. To this end, every class (that you can instantiate with an object) will have a special block of code called a constructor (sometimes there will be more than one) that describes all of the things that need to be done to create a new object of that class -- things that will done every time the constructor is invoked.
To use a constructor to create an object (typically that some variable will then reference) you need to use the "
new" keyword followed by the name of the class and then some parentheses which may or may not include some additional parameters the constructor might need to do its job.
So, for example, to create two new
JButton objects named
myButton2, we could write the following:
JButton myButton1 = new JButton(); \\new JButton with no text JButton myButton2 = new JButton("OK"); \\new JButton with text "OK"
Notice above, the second constructor above was passed information about what text would be displayed on the button, while the first constructor was not passed any information. What information we can pass to a constructor (and the order in which we must pass it) is something one can generally look up. For all of the classes in the standard libraries associated with Java, we can check the Java API (Application Programming Interface).
As with primitive types, you can split the declaration and instantiation/initialization up into two steps:
JButton myButton; myButton = new JButton();
Once we have a variable that references a specific object, we can access the data and actions associated with that object quickly and easily.
Consider a class named Rectangle. The "data" associated with a rectangle might consist of its height, width, and x & y coordinates. This data is stored in special variables called instance variables. To access these variables, we join the name of the variable referencing the object and the name of the instance variable with a dot ("."), as shown below:
Rectangle myRect = new Rectangle(3,4); int perimeter = 2 * myRect.height + 2 * myRect.width; System.out.println("The perimeter of myRect is " + perimeter);
Actions that objects can take are dealt with in a similar manner. Suppose the Rectangle class supports the ability to change the size (i.e., height and width) of the rectangle in question. This action corresponds to a method -- in this case, one named
To apply the action, we join the name of the variable referencing the object and the name of the method again with a dot ("."). So that the computer knows we are invoking a method (and not an instance variable of the same name), we must always put parentheses at the end of the method name.
Inside these parentheses, we may or may not specify additional information to be passed to the method that it needs to accomplish whatever action it is meant to perform.
As an example, the following code constructs a rectangle that is initially 30 pixels wide and 20 pixels high, and then changes its size to 50 pixels wide and 100 pixels tall:
Rectangle myRect = new Rectangle(30,20); myRect.setSize(50,100);