constructor in java_Constructors in Java

Java constructorsare special methods (without return type) which allow you to fully initialize the object state before it can be used by other classes inside application. Constructors in java are invoked using new

keyword.

Let’s learn about constructors in more depth.

What are Constructors in Java

Constructors are special method like (no exactly methods) constructs which helps programmer in writing object initialization

code, before the object is available for use by other objects in the application.

Whenever application needs a new instance of any class, JVM allocates a memory area inside heap. Then JVM executes the invoked constructor (class can have multiple constructors) and initialize the object state. Inside constructor, you can access all object attributes and assign them to their default values or any desired values.

Read More:Java Memory Model

Types of Constructors

Default Constructor (no-arg constructor)

In case, programmer does not provide any constructor in class definition – JVM provides a default constructor to the class in runtime.

Programmer also can override default constructor in class. Let’s look at the syntax.

public class Employee

{

public Employee() {

}

}

In default constructor, name of the constructor MUST match the class name and it should not have any parameters.

Parameterized Constructor via Constructor Overloading

As stated above, there can be multiple constructors inside a class. This is possible by having overloaded constructors. In constructor overloading

, you can pass list of arguments as per requirements i.e. how many ways a class can be initialized.

public class Employee {

private String firstName;

private String lastName;

public Employee() {//constructor 1

}

public Employee(String firstName) { //constructor 2

}

public Employee(String firstName, String lastName) { //constructor 3

}

}

In above class, we have defined 3 constructors to handle 3 situations – how the employee object might be required to create by the application i.e. without name, with first name only and with first and last name both.

Employee employee1 = new Employee();

Employee employee2 = new Employee("Lokesh");

Employee employee3 = new Employee("Lokesh", "Gupta");

Constructor Rules

There are few mandatory rules for creating the constructors in java.

Constructor name MUST be same as name of the class.

There cannot be any return type in constructor definition.

There cannot be any return statement in constructor.

Constructors can be overloaded by different arguments.

If you want to use super()

i.e. super class constructor then it must be first statement inside constructor.

Constructor Chaining

In java, it’s possible to call other constructors inside a constructor. It’s just like method calling but without any reference variable (obviously as instance is fully initialized as of now).

Now we can call constructors of either same class or of parent class. Both uses different syntax.

Call same class constructor

To call other constructors from same class, use this

keyword. For example,

public Employee() {

}

public Employee(String firstName) {

this();//calling default constructor

}

public Employee(String firstName, String lastName) {

this(firstName);//calling constructor with single argument of String type

}

Call super class constructor

To call constructors from super or parent class, use super

keyword. The usage of super keyword is similar to this

keyword – only difference is that super

refers to super class and this

refers to current instance.

public Employee() {

//refer to Object class constructor

//as it is parent class for every class

super();

}

Private Constructors

Sometimes you want to protect the constructor from being called by other classes. Altogether you want that nobody should be able to create a new instance of the class.

Why anybody would want that? Well, it’s necessary forsingleton pattern. In singleton, an application wants to have one and only one instance of any class.

A common singleton class definition looks like this:

public class DemoSingleton implements Serializable

{

private static final long serialVersionUID = 1L;

private DemoSingleton() {

// private constructor

}

private static class DemoSingletonHolder {

public static final DemoSingleton INSTANCE = new DemoSingleton();

}

public static DemoSingleton getInstance() {

return DemoSingletonHolder.INSTANCE;

}

protected Object readResolve() {

return getInstance();

}

}

That’s all about constructors in java

. Drop me your questions in comments section.

Happy Learning !!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值