Thursday 9 January 2014

Initialization and Constructors in Java





Initializing an means putting things into it. if you have an array of objects, the array does not hold the object but holds a reference to the objects. The default value of an element of an array which has a type of object is null, so be careful if you try to use this element otherwise you get a nullpointerexception error.

To access an element within the array you start from 0, if you get an ArrayIndexOutOfBoundsException it means that you are trying to access an element that does not exist (not within range).
# Setting values for sepcific elements within the array
Colors[0] = new Color();    // Object type
x[4] = 2;                     // primitive type

# Initializing a multidimensional array
array[0] = new init[4];     // Specify that element 0 holds an array of 4 int elements
array[1] = new init[6];     // Specify that element 1 holds an array of 6 int elements

array[0][0] = 5;
array[0][1] = 6;




Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e. on new ClassName()).

Constructors have no return type (not even void).
The constructor name must match the class name.
If you don’t define an explicit constructor, Java assumes a default constructor

The default constructor accepts no arguments.
The default constructor automatically invokes its base class constructor with no arguments, as discussed later in this module.
You can provide one or more explicit constructors to:

Simplify object initialization (one line of code to create and initialize the object)
Enforce the state of objects (require parameters in the constructor)
Invoke the base class constructor with arguments, as discussed later in this module.
Adding any explicit constructor disables the implicit (no argument) constructor.
We can add a constructor to our Employee class to allow/require that it be constructed with a name and a social security number:

class colors {
    String name;
    String x;
   
    colors(String name, String x) {
        this.name = name; // "this." helps distinguish between
        this.x = x; // instance and parameter variables
    }
   
}

As with methods, constructors can be overloaded.
Each constructor must have a unique signature.

The parameter type list must be different, either different number or different order.
Only parameter types determine the signature, not parameter names.
One constructor can invoke another by invoking this(param1, param2, …) as the first line of its implementation.
It is no longer possible to do Employee e = new Employee(); because there is no constructor that takes no parameters.

We could add additional constructors to our class Employee:

“Constant” fields are defined using the final keyword, indicating their values can be assigned only once.

Final instance fields must be initialized by the end of object construction.
Final static fields must be initialized by the end of class initialization.
Final local variables must be initialized only once before they are used.
Final method parameters are initialized on the method call.

No comments:

Post a Comment