Skip to main content

Private Constructor in java

It is very important point to know as a java developer. So normally constructor is a special method which is used to initialize the instance variables.we can apply public,protected,default,private modifiers to constructors.so here our question is what happens if apply private modifier to constructor the answer is ,if we apply private to constructor the object creation is not possible out side of the class.the object creation is possible only internally with in the class.

In some cases we will use private costructor
  • sigleton
  • static methods(utililty methods)
  • factory methods
Our main point is using private constructor we can restrict object creation out side of the class.
As we know we can use private constructor is used to create singleton class see the below
Example
package com.java2study;
 public class PrivateConstructorDemo {
 private static PrivateConstructorDemo p;
 private PrivateConstructorDemo() {
  super();
 System.out.println("private constructor"); 
 }
 public static PrivateConstructorDemo getInstace(){
  if(p == null){
   p = new PrivateConstructorDemo();
  }
  return p;
 }
}
package com.java2study;

public class Test {

 
 public static void main(String[] args) {
  PrivateConstructorDemo p1 = PrivateConstructorDemo.getInstace();
  PrivateConstructorDemo p2 = PrivateConstructorDemo.getInstace();
  PrivateConstructorDemo p3 = PrivateConstructorDemo.getInstace();
  System.out.println(p1.hashCode());
  System.out.println(p2.hashCode());
  System.out.println(p3.hashCode());
  System.out.println(p1.hashCode() == p2.hashCode());
  System.out.println(p2.hashCode() == p3.hashCode());
  System.out.println(p1.hashCode() == p3.hashCode());

 }

}
output
private constructor
640642021
640642021
640642021
true
true
true

Popular posts from this blog

Java Primitive DataTypes

Java data types will tell us what type of data can variable hold. Ex: int x = 10; In the above variable, x can hold int data type value.so we must declare any variable with data type before we can use a variable.

Instance Variables in Java

Instance variables should declare with in the class but outside of any method or block or constructor.instance variables can accessible inside class all methods , blocks & constructors.

Angular 9 Date Pipe Example

Table of Contents what is pipe in angular? Angular Date Pipe Pre-defined Date Format Custom Date Formats what is pipe in angular? In Angular Pipes are used to transform data.Pipes will take input data and transforms input data in desired output. Symbol of Pipe : " | " Angular Date Pipe Angular DatePipe is one of built-in Pipes.DatePipe is used for Format a date value according to locale rules. Syntax {{value_expression| date [:format[:timezone[:locale]]]}} DatePipe avalibale in '@angular/common' module. Example //app.component.ts import   {   Component   }   from   '@angular/core' ; @ Component ({   selector:   'app-root',    template:   `{{dOB | date:'fullDate'}}`,    styleUrls:  ['./app.component.css'] }) expo...