UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

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  

17 Answers

Learn Java

Follow 0
Answer

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

IT Professional Trainer with 15 years of experience in IT Industry

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

Real Time Java

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...
read more
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 } read less
Comments

Software Engineer

yes via singlton pattern
Comments

MCA

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

Tutor taking Computer subjects Classes

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

Industry expert and professional lecturer/trainer

Yes via Singleton Design pattern
Comments

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...
read more
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 read less
Comments

Trainer

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...
read more
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"); } } } read less
Comments

View 15 more Answers

Related Questions

I have the opportunity to learn either java or pega...what should i be targeting Pega or Java.. Also, if I m moving to Java. I will be getting immediate promotion and increment.
Java of course. You will see most of the enterprise level application are built in Java. Apart from retail, in every domain you'll see opportunity with Java
Gaurav
2 0
6
Why is finally introduced in Java?
finally is block which executes every time regardless exception has occurred or not. Finally is important in case you want to do some task which must be done like clean-up, db connection closure, memory de-allocation etc.
Agastya
What should I do after learning Java core?
After mastering Java core, consider expanding your skills by exploring more advanced Java topics such as: Object-Oriented Programming (OOP): Deepen your understanding of OOP principles and design patterns. Java...
Kumarnaik
0 0
5
What is the difference between Java and Advanced Java?
Java contains some Packages for developing standalone applications, we are use java for writing business logic in advanced java. Advanced Java is used for web development.
Anu

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

Ask a Question

Related Lessons

Spring - Dependency Injection (DI)
Spring - Dependency Injection (DI) DI is a framework which provides loose coupling in code. Here loose coupling means no hard coding of the object. Instead of hard coding, we will be injecting these object...

Java : Compile-time Versus Runtime optimization
While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below : What...
S

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...

Migration from other languages to Salesforce
Anyone can easily migrate from other language to Salesforce. People must have skills to understand business logic.

doWhile example in Java
public class doWhilePracticleEx { public void test() { } public static void main(String args) { String q1 = "Who is PM of India?"; String a1 = "Sonia Gandhi"; String a2 = "Rahul Gandhi"; String a3...
S

Sarthak C.

0 0
0

Recommended Articles

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more