
Creating Object Types
There are plenty of object types (like String and Point) created in Java and ready for you to use. These object
types are part of the Java API. However, Java wouldn’t be very useful unless you could create your own
object types. In fact, all the code you write in Java goes inside of the definition of an object type. Nothing
else is allowed. And believe it or not, you already created your first Java object type. What was it called?
public class HelloWorld ...
That’s right. HelloWorld was your first object type. An object type is called a class. A class defines what
data objects of that type can store and what functions they can perform. A class is a template for object
creation. An object is really an instance of a class. Many object instances can be generated from one class.
Exploring a simple class can help clarify what belongs in a class. Remember, in OO programming, you are
abstracting the real world, so in this case the class is a real-world object: a car.
public class Car{
}
By reading the first line of the class, you should be able to identify a few things.
• What file does this code belong in? Car.java
• What file will contain the Java bytecodes? Car.class
• How do you make one Car object? new Car()
• How do you make a Car reference? Car myCar
• How do you assign the Car object to a car reference? Car myCar = new Car()
As you continue to attempt to mimic the real world, you ask yourself, What are the parts of a car? In the
interest of keeping the example simple, only a few key instance variables are examined.
Color
Type (Sedan, Coupe, Convertible)
Each of the instance variables must be named and typed as shown below.
public class Car{
String color; //Just use a string to hold the color of the car
String type; //Sedan, Coup, Convertable
}
Each instance variable added follows a very similar pattern.
<type> <name>;
Actually, this is somewhat simplified. To be complete, you can include some optional information in the
instance variable definitions ([ ] denotes optional).
[<modifiers>] <type> <name> [ = initial_value] ;
Remember that the object types have data and behaviors or procedures. To give the car object type some
behavior, you need to add methods.
• Behavior 1: start() will print out “Car Started”.
• Behavior 2: printDescription() will print out a description of the car.
public class Car{
String color;
String type;
void start(){
System.out.println(“Car Started”);
}
void printDescription(){
System.out.println(“This is a ”+ color + “ “ + type);
}
}
As you can see, the code is pretty simple. You will see more complex code after you better understand the
concepts. To define a method, you must include four parts:
• Return type
• Name
• Argument list (parameter list)
• Implementation code block { }
Method definition syntax has a few optional elements as well. You will see how to use each as the class
progresses.
[<modifiers>] <return type> <name><( [<type> <name>] ) [<throws>] {}
You may note that instance variables are defined before the methods in the class. In fact, the order of instance
variables and methods is not important in Java. Conventionally, instance variables are listed near the top of
classes and before methods. Conventionally, instance variables, constructors (discussed below), and methods
are grouped together for easy location.
Working with this car class and objects of the Car type is simple!
Car myCar = new Car();
myCar.color = “white”;
myCar.type = “Sedan”;
myCar.printDescription();
myCar.start();
Use dot notation to access both object data and object methods.
Both methods for Car return a void return type. That means nothing is returned from this method. You could
change printDescription() to getDescription() and have this method construct and return a String object rather
than printing it out directly.
public String getDescription(){
String desc = “This is a ”+ color + “ ” + type;
return desc;
}
Without the return statement, this code would not compile. When you define a method to return a value
(String in this case), it must return that type.
The return statement terminates a method, so no code belongs after the return statement.
Using the return type, you can now substitute this method call anywhere you would normally use a String.
Car wifesCar = new Car();
wifesCar.color = “silver”;
wifesCar.type = “Sebring”;
String s = wifesCar.getDescription();
System.out.println(s);
//or more directly
System.ou
Creating Object Types
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services