|Navigate Classes and Objects topic: )|
Every class in Java can be composed of the following elements:
- fields or member variables — Fields are variables that hold data specific to each object. For example, an employee might have an ID number. There is one field for each object of a class.
- member methods — Member methods perform operations on an object. For example, an employee might have a method to issue his paycheck or to access his name.
- static fields — Static fields are common to any object of the same class. For example, a static field within the Employee class could keep track of the last ID number issued. Only one static field exists for one class.
- static methods — Static methods are methods that do not affect a specific object.
- inner classes — Sometimes it is useful to contain a class within another one if it is useless outside of the class or should not be accessed outside the class.
- Constructors — A special method that generates a new object.
- Parameterized types — Since 1.5, parameterized types can be assigned to a class during definition. The parameterized types will be substituted with the types specified at the class's instantiation. It is done by the compiler. It is similar to the C language macro '#define' statement, where a preprocessor evaluates the macros.
|Code listing 4.1: Employee.java
The following Java code would produce this output:
A constructor is called to initialize an object immediately after the object has been allocated:
|Code listing 4.3: Cheese.java
Typically, a constructor is invoked using the
|Code section 4.1: A constructor call.
The constructor syntax is close to the method syntax. However, the constructor has the same name as the name of the class (with the same case) and the constructor has no return type. The second point is the most important difference as a method can also have the same name as the class, which is not recommended:
|Code listing 4.4: Cheese.java
The returned object is always a valid, meaningful object, as opposed to relying on a separate initialization method. A constructor cannot be
synchronized. However, a constructor, like methods, can be overloaded and take parameters.
|Code listing 4.5: Cheese.java
By convention, a constructor that accepts an object of its own type as a parameter and copies the data members is called a copy constructor. One interesting feature of constructors is that if and only if you do not specify a constructor in your class, the compiler will create one for you. This default constructor, if written out would look like:
|Code listing 4.6: Cheese.java
super() command calls the constructor of the superclass. If there is no explicit call to
this(...), then the default superclass constructor
super(); is called before the body of the constructor is executed. That said, there are instances where you need to add in the call manually. For example, if you write even one constructor, no matter what parameters it takes, the compiler will not add a default constructor. The code listing 4.8 results in a runtime error:
|Code listing 4.7: Cheese.java
|Code listing 4.8: Mouse.java
This is something to keep in mind when extending existing classes. Either make a default constructor, or make sure every class that inherits your class uses the correct constructor.
Initializers are blocks of code that are executed at the same time as initializers for fields.
Static initializers are blocks of code that are executed at the same time as initializers for static fields. Static field initializers and static initializers are executed in the order declared. The static initialization is executed after the class is loaded.
|Code section 4.2: Static initializer.
Instance initializers are blocks of code that are executed at the same time as initializers for instance (non-
static) fields. Instance field initializers and instance initializers are executed in the order declared. Both instance initializers and instance field initializers are executed during the invocation of a constructor. The initializers are executed immediately after the superclass constructor and before the body of the constructor.