Home > DeveloperSection > Articles > Class Concepts in Java: Declaring Constructors

Class Concepts in Java: Declaring Constructors


Java Java  Class 
Ratings:
0 Comment(s)
 169  View(s)
Rate this:

Class Concepts in Java: Declaring Constructors 

 

A constructor is a special method that the runtime executes during the object-creation process. In previous parts, we saw that the following statement calls the class constructor:

Point p = new Point();

When we instantiate a class using the declaration shown here, an object is created with some default values assigned to its fields. However, we may want to create an object with an initial state that is different from the default state set by the compiler. For example, we might want to create a Point object with its initial values x and y set to 4 and 5, respectively. This can be achieved with the help of a constructor. To create a constructor, we write a method as shown here:

 

public Point()

{

   x = 4;

  y = 5;

}

 

·         This method has a name that is the same as the class name. This is one of the rules for defining a constructor.

·         Also, note that the method does not declare any return type, not even void. This is the second rule for defining the constructor—a constructor does not have a return type.

Now, let’s look at the other features of the constructor. The constructor in this case does not take any arguments. In general, a constructor can accept zero or more arguments. If the constructor does not specify an argument, it is called a no-argument constructor. Sometimes, this is also referred to as a default or a parameterless constructor.

In the constructor body, we put two assignment statements that initialize the values of the x and y fields to the desired values. We may assign any values we choose. When the object is created, it will have those values.

Once we write a constructor like this in the class definition, any time we instantiate the class, our constructor is called.

Point class with constructor methods

class Point

 {

          private int x;

          private int y;

 

          public Point() {

                   x = 10;

                   y = 10;

          }

 

          public int getX() {

                   return x;

          }

 

          public int getY() {

                   return y;

          }

}

 

Test Class:

class ConstructorTest

{

          public static void main(String[] args) {

                   System.out.println("Creating a Point object ... ");

                   Point p = new Point();

                   System.out.println("\nPrinting Point object");

                   System.out.println("Point p (" + p.getX() + ", " + p.getY() + ")");

          }

}

 

The class Point, just like in our earlier examples, declares two fields, x and y, of type int. The class also declares a constructor, as follows:

public Point() {

x = 10;

y = 10;

}

 

 

The constructor initializes both fields to a common value of 10. This is a no-argument constructor. The class also declares two getter methods for accessing the values of the two fields.

Note that we do not define any setter methods here because our program does not need to set the values of these fields to anything other than the default values set in the constructor.

In the main method of the ConstructorTest class, we instantiate the Point class, as follows:

Point p = new Point();

The execution of this statement results in calling the constructor we defined in the class definition. The constructor sets the values of both x and y fields to 10. We can verify this by dumping the object’s contents via the following statement:

System.out.println("Point p (" + p.getX() + ", " + p.getY() + ")");

Output:

Creating a Point object ...

Printing Point object

Point p (10, 10)

 


Don't want to miss updates? Please click the below button!

Follow MindStick