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

Is Java a compiler or Interpreter?
Java is a programming language and computing platform first released by Sun Microsystems in 1995. A Java compiler is a compiler for the Java programming language. A JVM interprets bytecode and...
Dr Rakesh
What is reflection and what is its use in Java?
to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time
Rahul
0 0
7
How do I learn Java? From book or internet or a coaching?
It is good to learn Java from Coaching where you will get more exposure in learning at faster pace.
Suresh
0 0
7
What is the general syllabus covered in core Java training from any institution?
you can check the syallbus on http://felix-its.com/java-training-pune/
Shiv Kohli
0 0
5

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

Ask a Question

Related Lessons

TestNG Annotations and its sequence
public class TestNGAnnotations { @BeforeMethod public void beforeM() { System.out.println("Before Method"); } @AfterMethod public void afterMethod() { System.out.println("After Method"); } @BeforeClass...
S

Sarthak C.

0 0
0

Constructor Overloading
public class ConstructorOverloading { public ConstructorOverloading(){ System.out.println("default"); } public ConstructorOverloading(String a){ System.out.println("a"); } public ConstructorOverloading(int...
S

Sarthak C.

0 0
0

What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0

Interview Tip : Q1) Why Strings are immutable in java ? What happen if it was mutable in java?
As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program. The answer to this...

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