
Java Constructors: Initialization and Usage Explained
Table of Content:
- Constructor
- Rules for creating Java constructor
- Types of Java constructors
- Java Default Constructor
- Syntax of default constructor:
- What is the purpose of default constructor?
- Example of default constructor that displays the default values
- Another Example of constructor
- Java parameterized constructor
- Why we use parameterized constructor?
- Example of parameterized constructor
- Related Questions
- Related Assignment
- Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.
Constructor
A constructor is a special kind of method that determines how an object is initialized when it’s created.
Rules for creating Java constructor
There are basically three rules defined for the constructor.
- Constructor name must be same as its class name
- Constructor must have no explicit return type. i.e Constructors do not have a return type — not even void
- Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
Types of Java constructors
There are two types of constructors:
- Default constructor (no-arg constructor)
- Parameterized constructor
Java Default Constructor
A constructor that have no parameter is known as default constructor.
Syntax of default constructor:
ClassName() { }
Example of Default Constructor
In this example, we are creating the no-arg constructor in the Sample class. It will be invoked at the time of object creation.
class Sample{ Sample(){ // constructor System.out.println("this is inside constructor"); } public static void main(String args[]){ Sample b=new Sample(); } }
Output:
this is inside constructor Press any key to continue . . .
Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
What is the purpose of default constructor?
Default constructor provides the default values to the object like 0, null etc. depending on the type.
Example of default constructor that displays the default values
class Student{ String name; int rollNo; void display(){ System.out.println(rollNo+" "+name); } public static void main(String args[]){ Student s1=new Student(); Student s2=new Student(); s1.display(); s2.display(); } }
Output:
0 null 0 null Press any key to continue . . .
Explanation:
In the above class,you are not creating any constructor so compiler provides you a default constructor.Here 0 and null values are provided by default constructor.
Another Example of constructor
class Demo{ int value1; int value2; Demo(){ // this is constructor value1 = 10; value2 = 20; System.out.println("Inside Constructor"); } public void display(){ System.out.println("Value1 === "+value1); System.out.println("Value2 === "+value2); } public static void main(String args[]){ Demo d1 = new Demo(); d1.display(); } }
Output:
Inside Constructor Value1 === 10 Value2 === 20 Press any key to continue . . .
Java parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Why we use parameterized constructor?
Parameterized constructor is used to provide different values to the distinct objects.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
class Student{ String name; int rollNo; Student(String n, int i){ name = n; rollNo = i; } void display(){ System.out.println(rollNo+" "+name); } public static void main(String args[]){ Student s1=new Student("Dhinchak Pooja", 1); Student s2=new Student("Hero Alom",2); s1.display(); s2.display(); } }
Output:
1 Dhinchak Pooja 2 Hero Alom Press any key to continue . . .
- Question 1: What is finalize() method?
- Question 2: What is the purpose of default constructor?
- Question 3: Can a constructor be made final?
- Question 4: What's the difference between constructors and other methods?
- Question 5: Can you call one constructor from another if a class has multiple constructors?
- Question 6: Can constructor be inherited?
- Question 7: Where and how can you use a private constructor?
- Question 8: What is constructor chaining and how is it achieved in Java?
- Question 9: What is the default constructor?
- Question 10: Rules for creating Java constructor
- Question 11: When is a constructor called
- Question 12: Types of Java constructors in Java
- Question 13: What is the purpose of a default constructor?
- Question 14: Why use the parameterized constructor in Java?
- Question 15: Difference between constructor and method in Java
- Question 16: Can constructor perform other tasks instead of initialization?
- Question 17: (v) Consider the following Java program and determine its output:
class report { int a, b; report() { a = 10; b = 15; } report(int x, int y) { a = x; b = y; } void print() { System.out.println(a * b); } public static void main(String[] args) { report r = new report(); r.print(); report p = new report(4, 5); p.print(); } }
Related Questions
- Assignment 1: Default constructor in java
- Assignment 2: Default constructor in java example 2
- Assignment 3: Default constructor in java, constructor present in another class
- Assignment 4: Parameterized Constructor, constructor present in another class
- Assignment 5: Parameterized Constructor, constructor present in same class
- Assignment 6: Parameterized Constructor, constructor present in same class, with method
- Assignment 7: Parameterized Constructor, constructor present in same class, with method example 2
- Assignment 8: Parameterized Constructor, constructor present in same class, with method usefull example