java constructor tutorial for beginners

JAVA constructor tutorial: JAVA constructor introduction

As we said in the JAVA Object Tutorial, the constructor is a special method defined in a JAVA class.

The main reason of creating this method is to initialize the attributes of an object.

This method is called when creating new objects to initialize their attributes.

It is called constructor because it is called when constructing objects.

There are rules that should be respected when creating a java constructor.

We will mention those rules in the next section.

JAVA constructor tutorial: JAVA constructor characteristics

A JAVA constructor must respect these rules:

·       JAVA constructor must have the same name of the class including capital letters

·       JAVA constructor doesn’t have a return type (even void is not permitted)

·       JAVA constructor is used mainly to assign values to the attributes of an object

The constructor gives an initial state to an object.

Then, when the program runs, this state will change.

There are other characteristics of java constructors that are related to concepts that we haven’t dealt with yet (inheritance,…).

We will deal with these concepts when we will deal with those concepts.

JAVA constructor tutorial: JAVA constructor types

We can define many constructors in a JAVA class by overloading constructors.

Check the java method overloading tutorial to know more about this subject.

But, in a class we have only one default constructor.

A Java class contains:

  • Only one default constructor
  • Zero or many overloaded constructors

The default constructor is the constructor which doesn’t have any parameter.

JAVA constructor tutorial: JAVA default constructor example

public class Vehicle {
String color;
int speed;
public Vehicle() {
this.color = "Black";
this.speed = 220;
}
}

The default constructor in the example above is the method:  public Vehicle()

If we don’t create a default constructor, the JAVA compiler creates an empty default constructor for us automatically.

But, if we don’t create a default constructor and we create an overloaded constructor, then the compiler will not auto-generate a default constructor for us.

So, we should create it by ourselves.

JAVA constructor tutorial: JAVA overloaded constructors example

We can add other overloaded constructors as well to the Vehicle class:

public class Vehicle {
String color;
int speed;
public Vehicle() {
this.color = "Black";
this.speed = 220;
}
public Vehicle(String color, int speed) {
this.color = color;
this.speed = speed;
}
public Vehicle(int speed) {
this.color = "White";
this.speed = speed;
}
public Vehicle(String color) {
this.color = color;
this.speed = 300;
}         
}

The JAVA constructor example above contains in addition to the default constructor three overloaded constructors.

The overloaded constructors are:

  • public Vehicle(String color, int speed) : allows to initialize all the attributes of an object of type Vehicle using custom values
  • public Vehicle(int speed) : allows to initialize the speed attribute of a vehicle object and gives a default value (“white”) to the color attribute
  • public Vehicle(String color) : allows to initialize the color attribute of a vehicle object and gives a default value (300) to the speed attribute

JAVA constructor tutorial: Creating objects using JAVA constructors

JAVA calls the appropriate constructor when we use the new operator to create objects.

The appropriate constructor to call is chosen based on its argument list.

We will present a java constructor example that uses the default constructor of the Vehicle class to create an object:

public class TestVehicle {
public static void main(String[] args) {
Vehicle v = new Vehicle();
System.out.println("speed = "+v.speed);
System.out.println("color = "+v.color);
}
}

The output of this program is the following:

speed = 220

color = Black

The java constructor example below uses the constructor that initializes all the attributes of an object of type Vehicle

public class TestVehicle {
public static void main(String[] args) {                   
Vehicle v = new Vehicle("Gray", 120);
System.out.println("speed = "+v.speed);
System.out.println("color = "+v.color);
}
}

The output of this program is the following:

speed = 120

color = Gray

The final java constructor example uses the constructor that allows to initialize the speed of a vehicle and gives a default value to the color attribute.

public class TestVehicle {
public static void main(String[] args) {       
Vehicle v = new Vehicle(320);
System.out.println("speed = "+v.speed);
System.out.println("color = "+v.color);
}
}

The output of this program is the following:

speed = 320

color = White

This java constructor tutorial for beginners arrives at its end. You can also check the oracle java objects tutorial.

Please don’t forget to subscribe to like our How to Program Facebook page or to subscribe to our newsletter to get our latest JAVA tutorials.

To check our core java tutorial, please check this link.

See you in the next core java tutorial.

Post a Comment

Previous Post Next Post