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  

Answer
Follow 0

Please enter your answer

17 Answers   3 Likes | 0 Dislikes

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
Dislike Bookmark

using factory method and using reflection concept

Comments
Dislike Bookmark

No, private constructor creation is not possible.

Comments
Dislike Bookmark

Join Urbanpro.com, India?s most trusted education portal for your learning requirements

POST YOUR REQUIREMENT NOW

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

Comments
Dislike Bookmark

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.

Comments
Dislike Bookmark

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 { //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(); } }

Comments
Dislike Bookmark

Software Engineer

yes via singlton pattern

Comments
Dislike Bookmark

BTech/MCA/BCA Tutor

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

Comments
Dislike Bookmark

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
Dislike Bookmark

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

Comments
Dislike Bookmark
Related Questions

IT Courses/Java

Why we are using this keyword in Java?

Asked by Kiran Bulkquestions 11/07/2017

Answer 7
Follow 5
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

What is the purpose of using throws keyword in java?

Asked by Saritha Bulkquestions 11/07/2017

Answer 10
Follow 8
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

Is synchronized keyword necessary, in Java, if everything that is modified in a function is local to that function?

Asked by Abishiek Bulkquestions 11/07/2017

Answer 5
Follow 1
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

What programming languages should a freshman learn if he already knows C, C++, Java and Python?

Asked by Sushmita Bulkquestions 11/07/2017

Answer 7
Follow 5
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

What is the use of the "this" keyword in Java?

Asked by Deepak Bulkquestions 11/07/2017

Answer 9
Follow 6
Answer submitted successfully. ×

Please enter your answer

Looking for a Tutor?

Find best Tutors in your locality on UrbanPro.

FIND NOW

Now ask questions in any of the 1500+ Categories, and Get Answers from Tutors on UrbanPro.com

ASK A QUESTION

Related Lessons

Method Overloading vs Method Overriding

1. Method Overloading: Method overloading means a same method with same name can have different implementations/body by changing one of the follwing thing: 1) Number of Parameters 2) Order of Parameters 3) Type of Parameter i. Method overloading can be...

Difference Between Checked And Un-Checked Exception

1. Exception is an abnormal condition in the program, if raises program terminates abnormally. 2. Exception always occurs at runtime only, there is no chance of exception at compile time. 3. Syntactical mistakes or errors are occurs at compile time. 4....

Final, Finally And Finalize In Java

Final: 1. It is a non access modifier that can be applied to variables,methods and to class. 2. Final means non-changable. Final variable: 1. If a variable is declared as final it can't be re-assigned. 2. If there is any final variable in class simply...

Implementation: What Is JPA And JPA?

The basic Java framework to access the database is JDBC. Unfortunately, with JDBC, a lot of hand work is needed to convert a database query result into Java classes. JPA allows us to work with Java classes as it provides a transparent layer to each database...

Why We Should Go With Java?

Java is widely used in the Desktop application, web application, mobile development, games etc. Java is an object-oriented programming language. Its follow all the oops concept Like Object, Class, polymorphism, Inheritance, Encapsulation, Abstraction...

Sponsored

Find Best Java Training Classes?

Find Now »