The Artima Developer Community
Sponsored Link

Java Buzz Forum
Private constructor in java example

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.
Private constructor in java example Posted: Jul 9, 2016 10:35 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Private constructor in java example
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

Constructor in java:

  • Constructor is used to initialize class variables.
  • Constructor will be executed once per object and whenever object is created constructor will be called.




Private Constructor in java:

  • We can make constructor private, public , protected and default.
  • If we define a constructor as private means we are restricting a class to create object  outside of the class.
  • A class having private constructor does not allow user to create object outside.
  • In singleton Design pattern we will use private constructor to restrict object creation


  1. package inheritanceInterviewPrograms;
  2.  
  3. public class A {
  4.   
  5.   private A(){
  6.        
  7.        
  8.    }
  9.  
  10. public static void main(String args[]){ 
  11.    
  12.       A s= new A();
  13.   }
  14.  
  15. }

  1. package inheritanceInterviewPrograms;
  2. //  www.instanceofjava.com
  3.  
  4. public class B {
  5.     
  6. public static void main(String [] args){
  7.      
  8.     A obj= new A();// Compile time error
  9.    
  10.  
  11. }
  12. }


private Constructor example

Read: Private constructor in java example

Topic: Java Tutorial : Java IO (what is java IO?) Previous Topic   Next Topic Topic: The JavaFX Print API

Sponsored Links



Google
  Web Artima.com   

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