Home » Java programming language

Differences between constructors and methods in Java

Java constructors vs methods: Here, we are going to learn what are the differences between constructors and methods in Java? Compare Constructor v/s methods in java? And, how constructor differs from methods in java?
Submitted by Preeti Jain, on July 26, 2019

Java methods

  • A method is used to explore the behavior of an object.
  • We can prefix access modifiers with methods.
  • A method must have a return type like a void, any primitives type (int, char, float, etc.), any Object type(Integer, Float, String, etc).
  • Java compiler does not call method implicitly we need to call explicitly.
  • Let suppose if we have a class and in that class, we don't have any single method so in that case java compiler does not include any method from its own by default.
  • It is not mandated to have the same name of methods as its class name or in other words, method name may or may not be same as of class name.
  • Methods are of two types:
    1. No argument methods
    2. Parameterized methods

i) No argument method

A method that has no parameter or argument or in other words this type of method does not take any argument is known as the no-argument method.

Example:

class NoArgumentMethod {
    // No argument method definition
    void display() {
        System.out.println("We are in no argument method");
    }
}

class Main {
    public static void main(String[] args) {
        // creating an object of NoArgumentMethod class
        NoArgumentMethod nam = new NoArgumentMethod();
        // calling display() method 
        nam.display();
    }
}

Output:

E:\Programs>javac Main.java

E:\Programs>java Main
We are in no argument method

ii) Parameterized method

A method that has parameters or in other words this type of method takes any argument is known as a parameterized method.

Example:

class ParameterizedMethod {
    String name;
    // Parameterized method definition
    void display(String name) {
        System.out.println("The name is " + name);
    }
}

class Main {
    public static void main(String[] args) {
        // creating an object of ParameterizedMethod class
        ParameterizedMethod pm = new ParameterizedMethod();

        // calling display() method 
        pm.display("Rahul");
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
The name is Rahul

Java constructors

  • A constructor is a special method which calls implicitly by JVM(Java Virtual Machine) at the time of object creation.
  • The purpose of a constructor is to initialize an instance variable of the object.
  • We can prefix access specifiers with constructor also.
  • A constructor must not have a return type i.e. it does not return anything.
  • A constructor is a special function which calls implicitly(i.e. it is good for the user that we don't need to call explicitly).
  • Let suppose we have a class named "Constructor" and in that class, we don't include any constructor so in that case java compiler will include a default constructor from its own.
  • It is mandated to have the same name as the constructor as its class name or in other words, constructor name must be same as the class name.
  • We have three types of constructors:
    1. Default Constructor
    2. Parameterized Constructor
    3. Copy Constructor

i) Default constructor

This default constructor calls implicitly by the compiler if we don't include any constructor in the class. Default constructor has no parameter or in other words no-argument constructor.

Example:

class DefaultConstructor {
    // Constructor defined
    DefaultConstructor() {
        System.out.println("We are in default constructor");
    }
}

class Main {
    public static void main(String[] args) {
        // Calling Default constructor
        DefaultConstructor dc = new DefaultConstructor();
    }
}

Output

E:\Programs>javac Main.java
E:\Programs>java Main
We are in default constructor

ii) Parameterized constructor

The constructor that has parameter is known as parameterized constructor. If we want to initialize instance variable with our values and this constructor does not call by compiler implicitly.

Example:

class ParameterizedConstructor {
    String name;
    // Constructor defined
    ParameterizedConstructor(String name) {
        this.name = name;
    }
}

class Main {
    public static void main(String[] args) {
        // Calling Parameterized constructor
        ParameterizedConstructor pc = new ParameterizedConstructor("Preeti");
        System.out.println("The name is :" + pc.name);
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
The name is : Preeti

iii) Copy constructor

A constructor that has one parameter and the parameter is the reference of the same class.

Example:

class CopyConstructor {
    String name;
    // Constructor defined
    CopyConstructor(String name) {
        this.name = name;
    }
}

class Main {
    public static void main(String[] args) {
        // Calling constructor
        CopyConstructor cc1 = new CopyConstructor("Preeti");

        // Copied cc1 constructor 
        CopyConstructor cc2 = cc1;

        System.out.println("The value of cc1 is :" + cc1.name);
        System.out.println("The value of cc2 is :" + cc2.name);
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
The value of cc1 is :Preeti
The value of cc2 is :Preeti





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.