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 holds the future: Java or Python?
The future success of Java or Python depends on various factors, including industry trends, project requirements, and developer preferences. Both languages are widely used, with Java being prominent in...
Nikhil
0 0
5
Need to learn java....
You may contact me. I teach Java in great depth and with very firm building blocks.
Ajay
what is the use of request get parameter map in servlet with example
Map of all input elements of form of Html can be retrieved with getParameterMap() method and it is very rarely used. Map requestParams = request.getParameterMap();
Pushpendra
How do I learn Java? From book or internet or a coaching?
According to me....u should start from basic Java..that is core Java... . Find coaching , who teaches Java for beginners.....then later u can go for advance java.
Suresh
0 0
7

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

Ask a Question

Related Lessons

ClassNotFoundException vs NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError It is an exception and happens due to programmer’s mistake and can be recovered by updating the code. Thrown when an application tries...

Java 8 Predicates
In the previous lession, we have learnt how to use filters and collectors. In filter we have passed the condition to evaluate whether the object is eligible to be filtered or not. Code given below for...

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

1.1. Reverse an array in Java.
public class Main { public static void main(String args) { int arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length / 2; i++) { int temp = arr; arr = arr; arr = temp; } System.out.println(Arrays.toString(arr)); }}

Java Achieve (JAR)
Java Archieve is a binary form to bundle a Java library which equivalent to zip file. It contains .class files, .properties, .jpeg, OR any supporting files to execute this library. For Example- junit.jar,...

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