Java Constructor with Example

Java Constructor with Example

The constructs (building blocks) of a Java class are variables, constructors, methods and some rarely used static blocks etc. Now let us discuss Java Constructor.

1. What is constructor or features of constructor?

A Java Constructor has the following two features.
  1. A constructor looks like a method but without any return type (even void should not exist).
  2. Constructor name and class name should be the same.

2. Why constructors are required?

Something must be executed at the start of execution of code, without the intervention of a Programmer, then a constructor is required.

3. What are the features or advantages of constructors?

  1. A constructor is called implicitly when an object is created. Incase of method, it should be called explicitly. Forgetting to call, the method is not executed. Regarding constructor, it is assumed practically, atleast one object is created in the code so that the constructor is called.
  2. Mainly used to initialize variables and objects in the code that can be used every method.
  3. A constructor should not be called like a method with an object. To call (or better word is access) the constructor, an object must be created.

4. What is default constructor?

A constructor without any parameters is known as default constructor because it is created and supplied into the program by the JVM if the Programmer does not provide one himself. Default constructor other name if no-args constructor.

5. Can we overload constructor like methods?

Yes, definitely. Like method overloading, constructor overloading (parameterized constructors) is also possible.

Following program uses a default constructor.

Java Constructor
Let us see the salient points of the code.

A Student class constructor is created without any parameters, known as default constructor. In the constructor, marks variable is initialized with a value of 80.

Student s1 = new Student();
Student s2 = new Student();

Two objects s1 and s2 are created and thereby two times the constructor is called. Observe the screenshot.

System.out.println("From main method: " + s1.marks);

marks variable is initialized in constructor and called from main() method. Likewise, the marks variable can be called from any method.

5 thoughts on “Java Constructor with Example

  1. sivarakesh

    class Rakesh
    int x=2;
    Rakesh(int x)
    void m()
    class Rakesh6
    public static void main(String[] args)
    Rakesh a=new Rakesh(5);

  2. Durvesh

    It maybe a silly question but I am confused by it. I want to know we can create variables then we can assign them values similarly we can create object and give it a value. So whats the difference between object and variable??

    1. S. Nageswara Rao, Corporate Trainer Post author

      A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed, which might be followed by parameters of any type.

      In Java, the same can be done with cloning.

      Cloning – Duplicating an Object – Marker Interface:

Leave a Reply

Your email address will not be published. Required fields are marked *