Creating Objects
In java, an object is a block of memory that will hold code and preform a specific task(s).
This is the foundation of a class that was discussed previous.
An object can be initialized (or as it's said instantiated) with
the keyword new. In order for an object to be used, it must be
created first.
Here is a general way to define an object:
Let's observe method 2 a bit more as this is most common. Similar to a regular variable,
this will initialize the object called name by invoking whats called a
constructor for that class. A constructor can be thought
of as a construction worker for a specific building. Without them, the building cannot be
built. The same applies to a class; without a constructor, there is no class that can be
used as an object.
Constructors
A constructor must be part of a class if you are going to use it as a type. It can also
be thought of as a method that will preform specific actions.
There are rules about constructors. First, the constructor MUST BE PUBLIC.
Second, it MUST MATCH THE CLASS NAME EXACTLY. Thirdly, it contains no other keywords such
as void, int, boolean etc.
Here is a class that contains a constructor:
A small example at that. What this will do is create a new instance of the object Example1.
Contained in the constructor is a simple welcome message that will print out "Welcome!" when
instantiated.
Instance variables
A class or object can have it's own variables. In Java, these are called
instance variables. By its definition, an instance variable is unique to each instance of
the class; so in general, each time a class is "instantiated" with the new operator, there is another
variable associated with it.
These variables are declared outside any methods you may have. These variables are declared
either public, private or protected. They are global to the class or
object they appear in. Let's see a small example.
Example 1:
Instance variables
Download source code here (Right click - Save Target As...)
In this example, the instance variable x is declared private. In addition, it is global to
the Variables object.
The program simply prints out "Welcome! 1" when the program starts.
Static variables
Static variables are quite a bit harder to understand. Do not be confused with a static variable
from C++. They are very different.
In java, a static variable (also called a
class variable), is a variable that is given a fixed block of memory.
The static keyword tells the compiler that there is exactly one copy of this variable in existence,
no matter how many times the class has been instantiated.
Let's just think of a real world example. Say you own a car. The car will always have four wheels
in the US. So if you made a class in java called Car, a variable called numWheels can be
made static since it will be the same for every car.
Let's see an example of a static variable.
Example 2:
Static variables
Download source code here (Right click - Save Target As...)
When you run the above program, this is the output:
Here is why the output is this way. With the s1 instance of the object,
the value of x is 1 and the value of y is 8, as per the arithmetic operations in the
constructor.
The next instance of the object SVariables is s1a. Here, the value of x is 2 BECAUSE
OF THE STATIC while the value of y is again 8, BECAUSE OF THE NON-STATIC.
In the final instance of the object SVariables, the x is 2 while the y is 8.