The Artima Developer Community
Sponsored Link

Java Buzz Forum
Can we create private constructor in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Can we create private constructor in java Posted: Feb 25, 2016 2:21 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Can we create private constructor in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Can a constructor in Java be private?
  • Yes we can declare private constructor in java.
  • If we declare constructor as private we can not able to create object of the class.
  • In singleton design pattern we use this private constructor. 

private constructor in java example program


 2.In what scenarios we will use private constructor in java.

  •  Singleton Design pattern
  • It wont allow class to be sub classed.
  • It wont allow to create object outside the class.
  • If All Constant methods is there in our class we can use private constructor.
  • If all methods are static then we can use private constructor.



 3.What will happen if we extends a class which is having private constructor.

  •  If we try to extend a class which is having private constructor compile time error will come.
  • Implicit super constructor is not visible for default constructor. Must define an explicit constructor

private constructor in java


Singleton Design pattern:


  1. package com.privateConstructorSingleTon;
  2.  
  3. public class SingletonClass {

  4. private static SingletonClass object;
  5.  
  6. private SingletonClass ()
  7. {
  8.         System.out.println("Singleton(): Private constructor invoked");
  9. }
  10.  
  11. public static SingletonClass getInstance()
  12. {
  13.  
  14. if (object == null)
  15. {
  16.  
  17. System.out.println("getInstance(): First time getInstance was called and object created !");
  18. object = new SingletonClass ();
  19.  
  20.  }
  21.  
  22. return object;
  23.  
  24. }
  25.  
  26. }
     

  1. package instanceofjava;
  2.  
  3. public class SingletonObjectDemo {

  4. public static void main(String args[]) {
  5.  
  6.      SingletonClass s1= SingletonClass .getInstance();
  7.      SingletonClass s2= SingletonClass .getInstance();
  8.      System.out.println(s1.hashCode());
  9.      System.out.println(s2.hashCode());
  10.  
  11. }
  12. }

Output:

  1. getInstance(): First time getInstance was called and object created !
  2. Singleton(): Private constructor invoked
  3. 655022016
  4. 655022016

Read: Can we create private constructor in java

Topic: Java Swing Layout Example Previous Topic   Next Topic Topic: Evolution of Systems Integration

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use