|
Second Summer 2002
|
Lecture Notes Three: Objects and classes, reference types,
symbolic names, variables declaration and their initialization
through assignment.
Objects and classes are central concepts for Java programming.
|
It might take you some time to master these concepts fully, but since
every Java program uses at least a couple of objects and classes, it is a good idea
to have a basic understanding of these concepts right away.
|
An object is an entity that you can manipulate in your program,
generally by calling methods.
|
You should think of an object as a "black box" with a public interface -- the methods you can
call -- and a hidden implementation -- the code and data that are necessary to make these methods
work.
|
Different objects support different sets of methods (in general).
|
OK, enough of that, let's see some examples. Have we seen any object yet?
|
System.out
|
What methods does it have?
|
println and print
| Yes, and we call them
by identifying the object, followed by the name of the
method, and then by parentheses. |
Yes, don't you ever forget the parentheses.
|
Good. What other objects have we seen?
|
None that I know of.
|
Exactly. You've seen one but you didn't know it was an object.
|
"Hello, World!" |
Indeed. It is a String object (its type).
|
Does every object have a type?
|
Yes.
|
Then what type does System.out have?
|
PrintStream and its class is defined in
the package java.io . But this shouldn't
tell you too much just yet.
|
Nor could I have answered this question by
myself with what we know so far.
|
Although you could have looked
it
up in the online documentation as part of class System
|
http://java.sun.com/products/jdk/1.2/docs/api/overview-tree.html
... which is defined in the package java.lang
(and there are so many other packages besides it).
|
Let's go back to "Hello, World!" .
|
What methods does it have?
|
No println or print , I don't think...
|
To find out what methods it supports we need to look up its
class (String )
into the on-line documentation.
|
Of all the methods it has, we choose to take
a look at the length() method.
|
For any object of type String the length method counts and returns (reports)
the number of characters in the string.
|
So "Hello, World!".length() evaluates to 13
|
... as the quotation marks are not counted.
|
I'd like to see that.
|
Very well, then try this:
| |
System.out.println("Hello, World!".length());
Same as before, only more work is to be done before println
can output its argument.
|
OK, let's move on. Without classes there would be no objects.
Let's take a look at classes.
|
A class is a holding place (or a container)
for static methods and objects.
|
That's old news: the Hello class holds the static
main method. The System class holds the
static out object.
|
A class is also a factory for objects. It contains the
blueprint of all objects of that kind, and can be used
to generate new objects.
|
I'd like to see that.
|
To see how a class can be an object factory, let us turn to another class: the
Rectangle
class in the Java class library.
|
Objects of type Rectangle describe rectangular shapes.
|
Note that a Rectangle object isn't a rectangular shape -- it is just
a set of numbers that describe the rectangle. Each rectangle is described by the
x and y coordinates of its top left corner, its width and height.
|
I think I'd like to see a picture of that.
|
|
We need to work an example first. You can make a new
rectangle with top left corner at (5, 10) and with
a width of 20 and height 30. To make a new rectangle you
need to specify these four values, and in that order.
|
|
new Rectangle(5, 10, 20, 30)
|
The new operator causes the creation of an object of type Rectangle .
|
The process of creating a new object is called construction.
|
The four values 5, 10, 20, 30 are called the construction parameters.
What does the new object look like?
|
Now you can draw your picture.
|
|
What's the rectangular shape that is described
by this Rectangle object?
|
|
Why did you draw the referential upside down?
|
Because that's how it is in computer graphics.
|
To find the x you just measure how far
you are from the left margin of the screen.
|
To find out the y coordinate you
measure how many lines of pixels are the in
between that point and the top of the screen.
|
This is a side-effect of thinking that in English
we write from left to right, and from top to bottom
|
... so a character in a text message could be located
by the number of the column in which it appears (the
x coordinate) and the line in which it
appears (the y coordinate).
|
To construct any object, you do the following:
- use the
new operator
- give the name of the class
- supply construction parameters (if any) inside
parentheses
|
Different classes will require different construction parameters,
and some classes will let you construct objects in multiple ways.
For example, you can also obtain a Rectangle object by
supplying no construction parameters at all:
|
new Rectangle()
But you must still use the parentheses.
This constructs a (rather useless) rectangle
with top left corner at the origin (0, 0),
width 0 and height 0.
|
How do I know that?
|
You have to read up the documentation see what
the designers of the class had in mind for it.
|
So it's not something I could have deduced, or inferred?
|
No.
|
What can you do with a Rectangle object?
|
What can you do with a number?
|
What number?
|
Say, "abc".length()
|
I could print it, to see if it comes out as 3 or not.
|
Can you print a Rectangle object?
|
I could try. Can it be printed?
|
You should try it.
|
I'd rather draw it.
|
We'll learn that in a few chapters. |
OK, how about this:
|
System.out.println(new Rectangle(5, 10, 20, 30));
How does it work?
|
The code creates an object of type Rectangle then passes the
object to the println method, and finally forgets that object.
|
To remember an object give it a name; hold it in an object variable.
|
An object variable is a storage location that stores not
the actual object but information about the object's location.
|
Can you give it any name?
|
Variable names in Java can start with a letter, an underscore (_ ),
or a dollar sign ($ ). They cannot start with a number. After the first
character, your variable names can include any letter or number.
|
Once you decide on a name for a variable, to declare it you need to place the name of the class
in front of it, followed by the variable name, and a semicolon (; ) at the end.
|
Like this? Rectangle a;
|
Yes. This is a declaration statement. It says that the name
a will be used for a variable that will hold the
address to a Rectangle object.
|
So far, the variable does not refer to any object at all. To make it
refer to an actual object you could copy in it the address of an actual
object reference, as returned by new :
|
Rectangle a = new Rectangle(5, 10, 20, 30);
It is very important that you remember that a does not
contain the object. It contains the address of the object (and refers to
this object).
|
I have a picture for that:
|
Very good.
|
You could have two object variables refer to the same object:
Rectangle b = a;
|
Yes, the equal sign (= ) acts more like an
arrow from right to left, as it represents the copying of
the value on the left into the location that the right hand
side denotes.
|
So the value on the right, which is the contents of the
variable a (an address) is copied into the
location that b denotes.
|
b has just been declared. And at the time
of its declaration (and also allocation) we copy in it
the address of the anonymous object that a
points to.
|
May I draw a picture of that?
|

Now how does the picture change if we add
Rectangle c = new Rectangle(5, 10, 20, 30);
|
A new object will come into the picture, and its address will be stored in the
variable with the name c .
|
Isn't it identical to the object pointed
to by a and b ?
|
Yes, it's like a twin. Identical but not the same.
|
Let me see it.
|
There you go.
|

What else can you do with Rectangle objects?
|
The Rectangle class has over 50 methods, some useful, some less so.
|
To give you a flavor of manipulating Rectangle , let us look at
a method of the Rectangle class.
| The translate method moves
a rectangle by a certain distance in the x and y directions.
|
For example a.translate(15, 25);
| ... moves the rectangle by 15 units
in the x direction and 25 units in the y direction.
|
Moving a rectangle doesn't change its width or height, but it changes
the coordinates of the top left corner.
|
Can I see that?
|
Let's write
a program and test it. As with the Hello program,
we need to carry out three steps:
|
1. Invent a new class, say RectangleTest
2. Supply a main method
3. Place instructions inside the main method
|
Correct. Let's see the program.
|
How about this one:
|
public class RectangleTest
{ public static void main(String[] args)
{ Rectangle a = new Rectangle(5, 10, 20, 30);
System.out.println(a);
a.translate(15, 25);
System.out.println(a);
}
}
It would work well, but if you try to compile it you will run into an error.
| |
frilled.cs.indiana.edu%pico RectangleTest.java
frilled.cs.indiana.edu%javac RectangleTest.java
RectangleTest.java:3: Class Rectangle not found.
{ Rectangle a = new Rectangle(5, 10, 20, 30);
^
RectangleTest.java:3: Class Rectangle not found.
{ Rectangle a = new Rectangle(5, 10, 20, 30);
^
2 errors
frilled.cs.indiana.edu%
|
Does it always come out in color?
|
Not really. But the point here is that for this program there is an
additional step that you need to carry out: you need to import
the Rectangle class from a package
|
... which is a collection of classes with a related purpose.
All classes in the standard library are contained in packages.
The Rectangle class belongs to the package
java.awt
|
The abbreviation awt stands for "Abstract
Windowing Toolkit". To use Rectangle from the
java.awt package simply place the following
| |
import java.awt.Rectangle;
|
at the top of your program.
|
You never need to import classes from the java.lang
package. All classes from this package are automatically imported.
|
That's why we can use the String and System
classes without ever needing to import them.
|
Ready for one final question?
|
Certainly.
|
What's the output of the following snippet of code?
| |
Rectangle a = new Rectangle(5, 10, 20, 30);
Rectangle b = a;
a.translate(10, 10);
b.translate(10, 10);
System.out.println(a);
|
Easy: a and b both point to the same object. We
basically translate it twice, once by using its a name and once
by using its b name.
|
In the end printing a or b is the same.
|
You will see the original rectangle that has been translated twice.
|
So the top left corner is now at (25, 30) but
the width and height are unchanged.
|
Good. Can we move on now?
|
We sure can.
|
Great. I think it was about time.
|
Last updated: Jun 14, 2002 by Adrian German for A201