TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Tutorials

    Java Singleton Pattern

    Overview

    Java Singleton pattern is used to make a class that must be created only once across the Java application.  In other words, a singleton class should have only one instance running in the JVM. Let’s learn how to convert a class into a singleton in this tutorial.

    Java Singleton Pattern

    /*
     * Java Singleton Pattern Demo Program
     * Java Tutorials - www.TestingDocs.com
     */
    
    public class JavaSingleton {
    
     private static JavaSingleton instance = null;
    
     // make the constructor private
     private JavaSingleton(){
     
     } 
    
     // getInstance() to ensure only one instance is created
     public static synchronized JavaSingleton getInstance(){
     if (instance == null)
     {
     instance = new JavaSingleton();
     }
     return instance;
     }
    
     public static void main(String arg[]){
     JavaSingleton first =new JavaSingleton();
     System.out.println("First Instance : "+ first.getInstance());
     JavaSingleton second=new JavaSingleton();
     System.out.println("Second Instance: " + second.getInstance());
     }
    }

     

    Java Singleton Pattern

    The constructor of the class is private, this is to prevent the clients from instantiating the objects of the class using the new operator.

    The static synchronized getInstance() method creates and returns an instance of the class. The method controls the creation of the object and returns the already created instance if it’s already created. The method creates an instance of the class during the initial call and keeps returning the same instance.

    Clients can invoke the method using the class name, For example:

    JavaSingleton.getInstance();

    Run the sample code and notice that the two objects of the class are exactly the same. We can invoke the getInstance() method multiple times, but this pattern ensures that only one instance of the class gets created in the Java program.

    —

    Java Tutorials

    Java Tutorial on this website:

    https://www.testingdocs.com/java-tutorial/

    For more information on Java, visit the official website :

    https://www.oracle.com/java/

    Related Posts

    Java Performance

    Java Tutorials /

    Improving Java Performance with Multithreading

    Download Greenfoot Windows

    Java Tutorials /

    Download & Install Greenfoot on Windows

    Java Tutorials /

    Java Static Code Analysis

    Java Tutorials /

    Java Testing Tools

    Java Tutorials /

    Handle Multiple Exceptions in Java

    ‹ J2EE Technologies› Checkstyle Source Code Analyzer Tool

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com