JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Constructors in Java

Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different. Constructor declarations look like method declarations—except that they use the name of the class and have no return type.

Syntax:

class Simple
{
    data member;
    void display(){
        // code to be executed
    }
    Simple() // constructor
    {
        // code to be executed
    }
}

Rules for writing Constructor:

It has following Rules

1. Constructor name must be the same as its class name.

2. A constructor in Java can not be abstract, final, static and Synchronized.

3. A Constructor must have no explicit return type that means constructor doesn’t return any value.

Types of constructors

There are two types of constructor available in Java:

Default constructor (no-arg constructor)

Parameterized constructor

default constructor

A constructor that has no parameter is known as the default constructor. If we don’t define a constructor in a class, then the compiler creates default constructor(with no arguments) for the class.

Syntax:

className(){}

Example of default constructor

class Simple
{
        Simple()
        {
            System.out.println("This is a default constructor");
        }
        public static void main(String args[]) {
            Simple obj = new Simple();
        }
}

Output:

This is a default constructor

Parameterized constructor

A constructor which has a specific number of parameters is called a parameterized constructor. That mean, A constructor that has parameters is known as parameterized constructor.

Syntax:

className(list of parameters){}

Example of parameterized constructor

class Employee {

    int empId;  
    String empName;  
            
    //parameterized constructor with two parameters
    Employee(int id, String name){  
        this.empId = id;  
        this.empName = name;  
    }  
    void displayInfo(){
            System.out.println("Id: "+empId+" Name: "+empName);
    }  
        
    public static void main(String args[]){  
        Employee obj1 = new Employee(45,"Peter");  
        Employee obj2 = new Employee(21,"Tony");  
        obj1.displayInfo();  
        obj2.displayInfo();  
    }  
}

Output:

Id: 45 Name: Peter
Id: 21 Name: Tony

Constructor Overloading

Like methods Overloading, we can also overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters.

Example of Constructor Overloading

class Student{  
    int id;  
    String name;   
    
    //creating one arg constructor  
    Student(int i)
    {  
        id = i;  
    }

    //creating two arg constructor  
    Student(int i,String n)
    {  
        id = i;  
        name = n;  
    }  
    void display()
    {
        System.out.println(id+" "+name);
    }  
    
    public static void main(String args[]){  
    Student s1 = new Student(11);  
    Student s2 = new Student(2,"Stark");  
    s1.display();  
    s2.display();  
    }  
}  

Output:

11 null
2 Stark

Difference between constructor and method in Java

Constructor Method
A constructor is used to initialized the instance variable of a class. A method is use for any general purpose processing or calculation.
A constructor must not have a return type. A method must have a return type.
A constructor name must be same as the class name. A method name may or may not be same as the class name.
A constructor is called only once at the time of Object creation A method(s) can be called any numbers of time.
A constructor is invoked implicitly. A method is invoked explicitly.