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

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

Comments
Dislike Bookmark

Real Time Java(SE and EE) Professional

Yes we can create actually it is implementation of singleton design pattern.The following is the example public class DataSource{ public static final DataSource object; private DataSource(){ //we can write any code like jdbc } static{ object=new DataSource(); } // you can have other methods according to requirement }

Comments
Dislike Bookmark

Software Engineer

yes via singlton pattern

Comments
Dislike Bookmark

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

POST YOUR REQUIREMENT NOW

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

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

BTech/MCA/BCA Tutor

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

Comments
Dislike Bookmark

Industry expert and professional lecturer/trainer

Yes via Singleton Design pattern

Comments
Dislike Bookmark

IT professional with over 8 years of hands on experience in Core Java & Telecom

Using Singleton design pattern. private constructors are defined when the project requires that only one object of a class exist. To implement this requirement 1. we will make the constructor as private 2. Create within the class a object in static block or just a private static global variable referring to the object of the class or in a public static method. 3. Have a public static getInstance method which will expose the only one object of the class created

Comments
Dislike Bookmark

Yes you can create Object of that class at anywhere by creating a public method that has the returned type as object and one more thing that Singleton pattern is nothing to do with creating an Object .The singleton Pattern we use is meant for one and only instance of that type in whole application consider the following code public class Alpha { private Alpha() { // no -args constructor } public Alpha createInstance() { return new Alpha(); } public static void main(String[]args) { Alpha a1= createInstance(); Alpha a2=createInstance(); } } Now If you want to create Object of Alpha class else where just make create Instance method as follow class Alpha { private Alpha() { // no -args constructor } public static Alpha createInstance() { return new Alpha(); } } public class Beta { public static void main(String[]args) { Alpha a1= Alpha.createInstance(); Alpha a2=Alpha.createInstance(); // Now check whether if those instance are same if(a1.equals(a2)) { System.out.println("Both instance are refering same object"); } else { System.out.println("Both instance are refering different object"); } } }

Comments
Dislike Bookmark

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

Comments
Dislike Bookmark
Related Questions

IT Courses/Java

Why we are using this keyword in Java?

Asked by Kiran Bulkquestions 11 Jul

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 Jul

Answer 10
Follow 7
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 Jul

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 Jul

Answer 7
Follow 4
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 Jul

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

Features Of Java

There is given many features of Java. They are also known as java buzzwords. The Java Features given below are simple and easy to understand. Simple. Object-Oriented. Portable. Platform independent. Secured. Robust. Architecture neutral. Dynamic. Interpreted. High...

JAVA Version History

Java Version History: There are many java versions that has been released. Current stable release of Java is Java SE 8. JDK Alpha and Beta (1995). JDK 1.0 (23rd Jan, 1996). JDK 1.1 (19th Feb, 1997). J2SE 1.2 (8th Dec, 1998). J2SE 1.3 (8th May, 2000). J2SE...

History Of Java

Java history is interesting to know. The history of java starts from Green Team. Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such as set-top boxes, televisions etc. For the green...

10 Cool SQL Optimizations That Do Not Depend On The Cost Model

Today, we don’t want to talk about cost based optimisation, i.e. optimisations that depend on a database’s cost model. We’ll look into much simpler optimisations that can be implemented purely based on meta data (e.g. constraints) and...

jOOQ 3.10 Supports JPA Attribute Converter

One of the cooler hidden features in jOOQ is the JPADatabase, which allows for reverse engineering a pre-existing set of JPA-annotated entities to generate jOOQ code. For instance, you could write these entities here: @Entity public class Actor { ...

Sponsored

Find Best Java Training Classes?

Find Now »