Java Programming/Defining Classes

Fundamentals
Every class in Java can be composed of the following elements:
 * fields, member variables or instance 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 or instance 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 or class 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. Each static field exists only once in the class, regardless of how many objects are created for that class.
 * static methods or class 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.

The following Java code would produce this output:

Constructors
A constructor is called to initialize an object immediately after the object has been allocated:

Typically, a constructor is invoked using the keyword:

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:

The returned object is always a valid, meaningful object, as opposed to relying on a separate initialization method. A constructor cannot be, , , , nor. However, a constructor, like methods, can be overloaded and take parameters.

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:

The  command calls the constructor of the superclass. If there is no explicit call to  or , then the default superclass constructor   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:

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
Initializers are blocks of code that are executed at the same time as initializers for fields.

Static initializers
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.

Instance initializers
Instance initializers are blocks of code that are executed at the same time as initializers for instance (non- ) 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.