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

I want to know what are the differences between runnable and thread class implementation except they are class and interface and how to decide which is better at what time ...
1) Implementing Runnable is the preferred way to do it. Here, you’re not really specializing or modifying the thread’s behavior. You’re just giving the thread something to run. That means composition is...
Ankit Garg
How can in prepare for competitive programming in Java ? Any person or Institute who Teaches ? HELP !
If you are already a java programmer and wanted to increase your logical skills and compete somebody then you have to practice writing various logics so that you will become strong in java programming....
Ajay
Why Java Laguage is not provided get method into Iterator? is there any specific reson.
1) Iterator is an Interface. 2) It's sole purpose is to iterate not save value. POJO's have getters and setters.
Niraj Raj
Why using Overriding in Java ?
There was a student who always pronounces "University" as "Univerkity". His teacher got angry and called the parents and asked to know the reason. Teacher got shocked as they are pronouncing "Capacity"...
Shivendra
Write a programme to replace the string without using replace command? This is my string. ..... s is replaced by th output is Thith ith my thtring
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; ...
Mithun

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

Ask a Question

Related Lessons

Introduction to Course Content
Video about what we are going to learn throughout the Java Training Session .

Why Should We Learn Java Language?
Most of starters has confusion today that which of the programming language should be chosen to work on. It totally depends upon your need. As per my opinion, if you want to grow your career in IT industry...

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

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

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

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