UrbanPro

Learn Java Training from the Best Tutors

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

Search in

What is a singleton class and when do you use it?

Asked by Last Modified  

10 Answers

Learn Java

Follow 3
Answer

Please enter your answer

7+ year of corporate Experience in Manual & Automation(Java + Selenium) Testing in IT industry

Singleton class have a private constructor. while we can't create an object from outside of class, we can create an object only once.
Comments

B.E. (IT)

One reason that tends to come up over and over again on the internets is that of a "logging" class (which you mentioned). In this case, a Singleton can be used instead of a single instance of a class because a logging class usually needs to be used over and over again ad nauseam by every class in a project....
read more
One reason that tends to come up over and over again on the internets is that of a "logging" class (which you mentioned). In this case, a Singleton can be used instead of a single instance of a class because a logging class usually needs to be used over and over again ad nauseam by every class in a project. If every class uses this logging class, dependency injection becomes cumbersome. Logging is a specific example of an "acceptable" Singleton because it doesn't affect the execution of your code. Disable logging, code execution remains the same. Enable it, same same. Misko puts it in the following way in Root Cause of Singletons, "The information here flows one way: From your application into the logger. Even though loggers are global state, since no information flows from loggers into your application, loggers are acceptable." Singleton candidate must satisfy three requirements: controls concurrent access to a shared resource. access to the resource will be requested from multiple, disparate parts of the system. there can be only one object. If your proposed Singleton has only one or two of these requirements, a redesign is almost always the correct option. For example, a printer spooler is unlikely to be called from more than one place (the Print menu), so you can use mutexes to solve the concurrent access problem. A simple logger is the most obvious example of a possibly-valid Singleton, but this can change with more complex logging schemes. read less
Comments

Tutor

Singleton class has only one instance and is generally meant for shortened finish of a program
Comments

Trainer

A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class
Comments

Trainer

A singleton class is one which has only one object of itself own which is a static object and outside the class we cant create another object as the constructor is private.
Comments

Talent with Professional Experience

Singleton class is a class which has one instance
Comments

2 Days Free Demo Classes

One reason that tends to come up over and over again on the internets is that of a "logging" class (which you mentioned). In this case, a Singleton can be used instead of a single instance of a class because a logging class usually needs to be used over and over again ad nauseam by every class in a project....
read more
One reason that tends to come up over and over again on the internets is that of a "logging" class (which you mentioned). In this case, a Singleton can be used instead of a single instance of a class because a logging class usually needs to be used over and over again ad nauseam by every class in a project. If every class uses this logging class, dependency injection becomes cumbersome. Logging is a specific example of an "acceptable" Singleton because it doesn't affect the execution of your code. Disable logging, code execution remains the same. Enable it, same same. Misko puts it in the following way in Root Cause of Singletons, "The information here flows one way: From your application into the logger. Even though loggers are global state, since no information flows from loggers into your application, loggers are acceptable." read less
Comments

Big Data Expert

A class is called singleton when it limits the number of object creation for that class to be one. We can't have more than a single object of that class. Singleton class are employed extensively in concepts like networking and database connectivity.
Comments

Java trainer , Data structure and algorithms

Singleton design pattern can be obtained by using following steps: 1.Private Constructor 2.Static Factory Method 3.Static Reference Object Creation As we are using a Static Reference Object Creation ,it is a "static" member and a static member can execute only one time and will create only one object. Creating...
read more
Singleton design pattern can be obtained by using following steps: 1.Private Constructor 2.Static Factory Method 3.Static Reference Object Creation As we are using a Static Reference Object Creation ,it is a "static" member and a static member can execute only one time and will create only one object. Creating only one Object for the entire class saves the memory space and which helps to not to degrade the performance, which is most important. Since we are able to create exactly one instance for one class ,it is called as Singleton class read less
Comments

Trainer

singleton is a design pattern that restricts the instantiation of a class to one object.
Comments

View 8 more Answers

Related Questions

What are the advance features in java 1.8 when compare to java 1.6?
Many are there. 1. Lambda expression (functional Programming). 2. Default methods in interfaces.
Naresh N
What's New in JDK 8 ?
1.Lamda Expressions I started with lambda expressions as this is probably the most sought after feature in the language after probably Generics/Annotations in Java 5. 2.Generic Type changes and improvements Taking...
Ashish
what is serialVersionUID , why it different for each bean? serialVersionUID = 42L;
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized...
Srilatha N
best source to learn java from?
Main source if information on Java is oracle documentation. But it takes your time to read through, a lot more than you expect. Better prefer a trainer who can give what you need.
Himanshu

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

Ask a Question

Related Lessons

Comparable vs Comparator
java.lang.Comparable java.util.Comparator For comparing some other object with its own object. Ex. I am comparing myself to some other employee. Method signature is: int compareTo (T object). For...



Class, Object and Methods in JAVA
Lesson-1: Class, Object and Methods of Java can be compared with body, heart and brain. Without these 3 things, Java does not exist. If you Google the above 3 items, you will find many definitions. But...

Class and Objects in Java
Class is a template or a blueprint which is used to describe an object. On other hand Object is a reference of a class which follows all the stuff written inside the class. How about taking the whole tour in the following video

Recommended Articles

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 >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

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 >

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