[Java] Constructor

Back-End/Java_1 2016. 5. 17. 14:00
donaricano-btn

Constructor

- Constructors are required to create objects for a class. Constructors are used to initialize the instance variable of an object

- It must have the same name as that of the class and have no return type

- If you don't define a constructor, then the compiler creates a default constructor

- Use this() to communicate from one constructor to another constructor in the same class

- Use super() to communicate with super class constructor


1. Default Constructor

- You can also call a constructor without parameters as default constructor because all of its class instance variable are set to default values


2. Constructor Overloading 

- Like method overloading we can overload constructors also


3. Constructor chaining

- Calling another constructor in the same class from another constructor is called constructor chaining.

By using this() we can call another constructor in the same class

this() should be the first line the constructor


4. SingleTon 

- We can make constructor as private. so that we can not create an object outside of the class

Singleton Pattern helps us to keep only one instance of a class at any time





'Back-End > Java_1' 카테고리의 다른 글

[Java] JDK, JRE, and JVM  (0) 2016.06.03
[Java] Exception  (0) 2016.05.18
[Java] Enum  (0) 2016.05.13
[Java] Array  (0) 2016.05.12
[Java] Static  (0) 2016.05.11
블로그 이미지

리딩리드

,