Programming





Constructors in Java

Constructors in Java


by Administrator | 02-Apr-2020
Java

Tags:    Constructor  Core Java  Difference

Share: 


A constructor is a block of code that is invoked when a class is instantiated. A constructor looks like a method but it does not have a return type.

Syntax

[Modifier] <Constructor-Name> ([<Parameters>]) [Throws] {
    //Statements…
}

Rules for Constructors

  1. The name of a constructor must be same as the class name.
  2. A constructor cannot have a return type.
  3. A constructor may or may not have an argument.
  4. A constructor can throw an exception like a method.
  5. Constructors can have any access modifier (private, default, protected, public), other modifiers cannot be used.
  6. Constructors are used to create an object by calling new on that class and to perform initialization tasks.
  7. The compiler will always provide a no-arg constructor if the coder is not writing any constructor explicitly.
  8. If you’re providing a constructor explicitly inside a class then the compiler will not provide the default no-arg constructor. If you need a no-arg constructor then you need to write it on your own.
  9. Constructors can be overloaded.
  10. The modifier of the default no-arg constructor will be the same as the class.

Types of constructor

  1. No-arg constructor

A constructor that does not have a parameter is known as a no-arg constructor. The compiler provides a no-arg constructor if the programmer has not written any constructor in a class.

public class Employee {
        private int id;
       private String name;
        public int getId() {
              return id;
       }
        public void setId(int id) {
              this.id = id;
       }
        public String getName() {
              return name;
       }
        public void setName(String name) {
              this.name = name;
       }
        // Overriding toString method to show the output
       @Override
       public String toString() {
              return "Employee [id=" + id + ", name=" + name + "]";
       } 
       // Writing a parameterized constructor to initialize id and names
       public Employee() {
              System.out.println("No-arg Constructor");
       }
        public static void main(String[] args) {
               Employee employee = new Employee();
              System.out.println(employee.toString());
       }
}
  1. Parameterized constructor

A parameterized constructor has a parameter to it. A parameterized constructor is always written by the programmer.

public class Employee {
        private int id;
       private String name;
        public int getId() {
              return id;
       }
        public void setId(int id) {
              this.id = id;
       }
        public String getName() {
              return name;
       }
        public void setName(String name) {
              this.name = name;
       }
        // Overriding toString method to show the output
       @Override
       public String toString() {
              return "Employee [id=" + id + ", name=" + name + "]";
       }
        // Writing a parameterized constructor to initialize id and names
       public Employee(int id, String name) {
              this.id = id;
              this.name = name;
       }
 
       public static void main(String[] args) {
               Employee employee = new Employee(1, "John");
              System.out.println(employee.toString());
       }
}

What is the difference between Constructor and Method?

Constructor

Method

A constructor is used to instantiate an Object with some initial values.

A method is a block of code that helps the user achieve specific functionality. The functionality of a method is not pre-defined like a constructor.

A constructor must not have a return type.

A method must have a return type. The method should have a void return type even if it’s not returning anything.

Constructors are invoked implicitly.

Methods are invoked explicitly.

The java compiler provides a default no-arg constructor if you don't explicitly write any.

Methods are user-defined.

A constructor name must be the same as the class name.

Method names may or may not be the same as the class name.

The modifier of the default no-arg constructor will be same as the class

There is no such rule for methods.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.