Constructors




Constructors are like methods, but are not methods. A class is required to have at least one constructor, and if you don't define one yourself, the Java compiler will create a default constructor (no parameters) for you in the class file that it creates. For example, if you have a class called MyClass.java, and you do not write a constructor yourself in the class, the Java compiler will create a default constructor and put it in the MyClass.class file (i.e. in the byte code file that the compiler creates). The constructor is called after the new command allocates RAM memory and loads the class into that RAM memory. We say that you are instantiating an object.

For example, if you say
new Integer(5);
in your code, the following steps occur:

Step 1: The keyword new allocates RAM memory for the object and loads the Integer class into the RAM memory.
Step 2: All instance variables are set to their default values.
Step 3: The constructor is called.
Step 4: The memory address (RAM storage location) of the object is returned.


The purpose of a constructor is for you to initialize your instance variables and do any other setup that is required for your object.

There are two main rules to follow when you write your own constructors (and you should write your own constructors.

Rule 1: The name (identifier) of the constructor must be the same name as the class name
Rule 2: There is no return type (not even void)


Examples:



Integer x = new Integer(5);

String s = new String("Hello World");



Example class with 1 constructor


// ******************************************************
// ******************************************************

// The following MyPoint class holds the coordinates
// for a point on a 2D plane.

public class MyPoint {

    // instance variables
    int x;
    int y;
    
    // constructor MyPoint with 2 parameters
    // this.x refers to the instance variable x
    // x refers to the temporary parameter variable x
    public MyPoint(int x, int y)    
    {
        this.x = x; 
        this.y = y;
    }
    
    // other methods not shown
    
    
}


If we say in our program (that uses this class):

MyPoint point = new MyPoint(5,7);

the following steps occur:

Step 1: The keyword new allocates RAM memory for the MyPoint object and loads the MyPoint class into the RAM memory.
Step 2: All instance variables are set to their default values. (i.e. x and y will be set to 0)
Step 3: The constructor is called and instance variable x receives 5 and instance variable y receives 7.
Step 4: The memory address (RAM storage location) of the MyPoint object is returned.
Step 5: The memory address (RAM storage location) of the MyPoint object is assigned to the variable point.
        (variable point now knows the memory address of our MyPoint object)
		
		


Example class with 2 constructors // ****************************************************** // ****************************************************** // The following Board class represents a board like you // might have for a checkers game, chess game, connect 4 game, // minesweeper game, etc. // Details are left out to keep it simple. An actual class // that represents a board would have more instance variables // and methods. public class Board { char [][] board; // 2 constructors follow // default constructor Board (no parameters) public Board() { // instantiate (new) the board 6x7 and put spaces into each cell board = new char[6][7]; clearBoard(); } // constructor Board with 2 parameters public Board(int rows, int cols) { // instantiate (new) the board and put spaces into each cell board = new char[rows][cols]; // now put spaces into each cell of the board clearBoard(); } // set the value public void setCellValue(int row, int col, char value) { board[row][col] = value; } // return the value of the cell public char getCellValue(int row, int col) { return board[row][col]; } // set all spaces to a space char ' ' public void clearBoard() { for (int r=0; <board.length; r++) { for (int c=0; c<board[r].length; c++) { board[r][c] = ' '; } } } // other methods are not shown }