| An object is a distinct instance of its class (an actual occurrence).
It has its own set of variables (known as "instance variables") and
methods (known as "instance methods"). When called, an object's
instance methods automatically act upon its instance variables
Java program creates many objects from a variety of classes.
|
| |
| These objects interact with one another by sending each
other messages. Through these object interactions, a Java
program can implement a GUI, run an animation, or send and
receive information over a network. Once an object has completed
the work for which it was created, it is garbage-collected and
its resources are recycled for use by other objects. |
| |
| |
| Creating Objects |
| In Java, we create an object by creating an instance of a class or, in other words, instantiating a class. |
| |
| Ex: |
| Rectangle rect = new Rectangle(); |
| |
| |
| The above single statement performs three actions: |
| |
| 1. Declaration: Rectangle rect is a variable declaration
that declares to the compiler that the name rect will be used
to refer to a Rectangle object. Here a class name is used as the variable's type. |
| |
| 2. Instantiation: new is a Java operator that creates the new
object (allocates space for it). |
| |
| 3. Initialization: Rectangle() is a call to Rectangle's
constructor, which initializes the object. |
| |
| |