SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Apex

Apex static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your APEX code

  • All rules 97
  • Vulnerability2
  • Bug26
  • Security Hotspot3
  • Code Smell66
Filtered: 1 rule found
singleton
    Impact
      Clean code attribute
        1. Singleton classes should have private constructors

           Code Smell

        Singleton classes should have private constructors

        consistency - conventional
        maintainability
        Code Smell
        • design-pattern
        • singleton

        This rule raises an issue when a class implements the Singleton pattern with a getInstance() method but has a public constructor that allows multiple instances to be created.

        Why is this an issue?

        How can I fix it?

        More Info

        The Singleton design pattern ensures that only one instance of a class exists throughout the application’s lifecycle. This pattern is useful when you need to coordinate actions across the system or manage shared resources like configuration settings or database connections.

        When a Singleton class has a public constructor, it defeats the pattern’s core purpose. Multiple instances can be created directly using the new keyword, bypassing the controlled instantiation mechanism. This leads to several problems:

        • Inconsistent state: Different instances may hold different data, causing unpredictable behavior
        • Resource waste: Multiple instances consume unnecessary memory and processing power
        • Broken assumptions: Code expecting a single instance may malfunction when multiple instances exist
        • Difficult debugging: Issues become harder to trace when multiple instances interact unexpectedly

        A proper Singleton implementation uses a private constructor to prevent external instantiation and provides controlled access through a static method.

        What is the potential impact?

        Multiple instances of a Singleton class can cause data inconsistency, resource waste, and unpredictable application behavior. This can lead to difficult-to-debug issues and compromise the reliability of systems that depend on the Singleton’s unique instance guarantee.

          Available In:
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories

        © 2025 SonarSource Sàrl. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use