After completion of this chapter, you should be able to
thisreference in your class definitions
Each instance of a class has its own copy of instance variables. For example:
Temperatureclass defines a
Temperatureclass can have a different value stored in its
Instance methods require that a new instance of a class be created in order to be used.
Instance methods typically interact with instance fields or calculate values based on those fields.
Static fields and static methods do not belong to a single instance of a class.
They are also known as class fields or class methods.
To invoke a static method or use a static field the class name, rather than the instance name, is used.
Class fields are declared using the
static keyword between the access specifier and they field type.
The counter field is initialized to 0 only once, regardless of the number of times the class is instantiated.
Primitive static fields are initialized to 0 if no initialization is performed.
Example: Declare a static class variable
Example: Use the Temperature class
Methods can be declared as static by placing the
static keyword between the access modifier and the return type of the method.
When a class contains a static method, it is not necessary to create an instance of the class in order to use the method. Instead, use the name of the class followed by a dot
Static methods are convenient because they may be called at the class level.
They are typically used to create utility classes, such as the
Math class in the Java Standard Library.
However, static methods may not access instance fields, only static fields.
Java allows methods to be overloaded. Overloading occurs when a class contains more than one method with the same name.
Overloaded methods must differ in the number or types of parameters.
Overloading is best used for methods that perform essentially the same operation. The advantage of overloading: Fewer method names to remember.
Example: method overloading, class
Example: using an overloaded method
Java uses the method signature (name, type of parameters and order of parameters) to determine which method to call.
This process is known as binding.
The return type of the method is not part of the method signature.
Quite often we may wish to allow users of our class to use different constructor methods useful when instantiating a
Since all constructor methods in a class must have the same name, that is the name of the class itself, we must overload this name if we want different methods.
Fraction class, for example, we may wish to allow users to use constructor methods to instantiate a default fraction, a whole number fraction, etc.
We now have three ways to construct new
Fraction object variables
Java automatically provides a default no argument constructor for a class if a constructor is not explicitly written.
The default constructor provided by Java:
We, as programmers, can provide our own no-argument constructor to override the behavior of the default no-argument constructor.
If any parameter constructor is written, we should also write a no-argument constructor.
If we write a no-argument constructor, we should provide the initialization of all instance fields.
The declaration of the object variable doesn't actually store the object's data. Instead, it will store a reference (pointer) to the object's data.
new keyword causes the instance of the object to be created in memory.
When assigning objects to another, the object names are said to be aliases for the same object
To indicate that an object variable doesn't currently reference any object instance, the variable can be assigned the value
Object reference variables can be passed to methods as parameters.
Java passes all parameters by value, so when an object is passed as a parameter, the value of the reference variable is passed.
The value of the reference variable is a reference to the object in memory. A copy of the object is not passed, just a pointer to the object.
Methods are not limited to returning the primitive data types.
Methods can return references to objects as well.
Just as with passing parameters, a copy of the object is not returned, only its address.
equals method is defined for all classes, but in many cases you will want to overload the
equals method with your own version of
One could then use the
equals method in a main method
toString method is also defined for all classes, but you will also want to write your own
One could than use the
toString method in a main method. Leaving off the .toString() inside a println statement will automatically invoke the toString method.
This is equivalent to invoking it explicitly, but the latter is rarely done.
A copy constructor is a constructor that takes an existing object of the same class as its argument and makes the new object an exact copy of the argument.
For example, the copy constructor for the Fraction class would be
To use the copy constructor to instantiate a new Fraction object would look like
this reference is simply a name that an object can use to refer to itself. It is available for use in all non-static methods.
this reference can be use to overcome shadowing and allow a parameter to be the same name as an instance variable.
this reference can also be used to call a constructor method from another constructor method.
A class definition may have other classes defined within them. These inner classes have unique properties:
For more information on inner classes, see examples
InnerClassDemo.java (Figures 6-26 and 6-27) in textbook.
New to Java 1.5 are enumerated data types. Known as an
enum, you can use an
enumerated data type to create variables that can hold only the values that belong to the enumerated data type.
Enumerated data types require a definition and then variable declarations just like a regular class
enum is a specialized class
For more information on enum types and variables, see examples EnumDemo.java, CarType.java, CarColor.java, SportsCar.java, SportsCarDemo.java in your textbook.
You can use
enums as switch arguments in Java 1.5
Creating an instance of one class as a reference in another class is called object aggregation.
Aggregation creates a "has a" relationship between objects. In the UML class diagram below, the course class has a Instructor and has a TextBook
Collaboration occurs when two classes interact with each other.
If an object is to collaborate with another object, it must know something about the second object, its methods, and how to call them
Telling one object about another object is most easily done when an object is instantiated.
The hard part of object-oriented design is determining the classes that will make up your program, their data and actions, and the interactions between the classes. A popular technique in the beginning stages of OO design is by creating CRC cards. CRC stands for class, responsibilities, and collaborations