Signup as a Tutor

As a tutor you can connect with more than a million students and grow your network.

IT Courses/Java

If a class contains only a private constructor and no other constructor, is it possible to create object of that class? If yes how?

Asked by Last Modified  

Follow 0
Answer

17 Answers

Please enter your answer


Software Developer, Expertise in Java/J2ee Technology.

Yes, It is possible. But, Object can be created only with in that class (either in any method or block).
Comments

using factory method and using reflection concept
Comments

No, private constructor creation is not possible.
Comments

Yes possible, but only within the class in which private contructor is written
Comments

We can have private contructor also. but using this we can create object with in the class itself, you can not create object from out side of the class. This process can be followed in singleton design pattern by providing a public method to get the same instance which had been created by static block.... read more
We can have private contructor also. but using this we can create object with in the class itself, you can not create object from out side of the class. This process can be followed in singleton design pattern by providing a public method to get the same instance which had been created by static block. I would say don't make the constructor private, don't make the build code under the constructor (place it in a new method, which can be private) and make a method to return the object outside the class. read less
Comments

Computer

YES WE CAN CREATE OBJECT FOR THAT CLASS WITH IN THE SAME CLASS. OUT SIDE OF THE CLASS IT IS NOT POSSIBLE. Example:- Step 1 you have to declare static variable Step 2 private construct . Step 3 access the object with static method Just like Class.forName(); public class SingleTonClass { ... read more
YES WE CAN CREATE OBJECT FOR THAT CLASS WITH IN THE SAME CLASS. OUT SIDE OF THE CLASS IT IS NOT POSSIBLE. Example:- Step 1 you have to declare static variable Step 2 private construct . Step 3 access the object with static method Just like Class.forName(); public class SingleTonClass { //Static Class Reference private static SingleTonClass obj=null; private SampleSingleTonClass(){ } public static SingleTonClass objectCreationMethod(){ /*This logic will ensure that no more than * one object can be created at a time */ if(obj==null){ obj= new SingleTonClass(); } return obj; } public void display(){ System.out.println("Singleton class Example"); } public static void main(String args[]){ //Object cannot be created directly due to private constructor //This way it is forced to create object via our method where //we have logic for only one object creation SingleTonClass myobject= SingleTonClass.objectCreationMethod(); myobject.display(); } } read less
Comments

Software Engineer

yes via singlton pattern
Comments

BTech/MCA/BCA Tutor

Private constructor can be created but it would not be effective as it could not be invoked .
Comments

If you declare a constructor as private, only code within the class itself can instantiate an object. The common cases of using the private constructors are Classes with only static methods Classes with only constants When implementing singleton pattern Enums
Comments

Private constructor creation is not possible.. if constructor create with private access specifier, then class can not create objects..
Comments

View 15 more Answers

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons


How to create excel sheets using Java?
HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet1 = workbook.createSheet("actions");for (int i = 0; i < 10; i++) { sheet1.createRow(i); for (int j = 0; j < 10; j++) { HSSFRow currRow = sheet1.getRow(i); currRow.createCell(j); HSSFCell...
Definition of Class and Object in Java
Object - Objects have states and behaviors. Example: A dog has states - colour, name, breed as well as behaviors - barking, eating, sleeping. An object is an instance of a class. Class - A class can be defined as a template/blueprint that describes the...
Hibernate 5
The Hibernate team published the first Hibernate 5 release quite a while ago, and since then they introduced a bunch of new features. I explained several of them here on the blog, and it’s about time to have a look at the most popular ones. Based...
Find Oracle Java Certification near you

Looking for Java Training Classes?

Find best Java Training Classes in your locality on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
Sponsored

Find Best Java Training Classes?

Find Now »