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

Which is better, PHP or Python? Why?
Python . Simple syntax,More readable,General purpose programming language
Kartheek
0 0
5
How can I execute a Java program?
Through command-line 1.compile the code using javac filename.java 2. Finally run the code using java filename here filename is classname. If u use eclipse then right click the java code n select run as java application .
Suresh
0 0
5
How do people become Java programmers?
People become Java programmers through various paths. Common steps include learning the basics of programming, gaining familiarity with Java syntax and concepts, and then practicing coding through hands-on...
Rishav
0 0
5
Which one is faster, Python or Java?
The speed of Python and Java can vary depending on the specific task. Java is generally considered faster for low-level operations and large-scale applications, while Python is often valued for its simplicity...
Chetana
0 0
5

I am a fresher and I want to become a Java developer, so what is necessary to crack the Java interview ? 

Hi Dipesh, understand that Java is a kind of ocean. It is vast. For interview preparation, you can brush up your Core Java Concepts and OOPs concepts and understand how JAVA works and learn the java features...
Dipesh

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

Ask a Question

Related Lessons

Java: A Quick Overview
Not purely Object Oriented: It doesn't support multiple inheritence, it supports primitive data types and static members. Doesn’t support multiple inheritance: Reason is diamond problem i.e.,...

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

What Is Java? Explain The History Of Java
i. Ovierview: Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java...

Java Advantages
In this video, learn about Java and its advantages. Also, check out the difference between Java and C++, Java development kit, Java Run Time Environment (JRE) with a proper demonstration program for better clarity.

Puzzle Game coding
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import...

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