Is this a good and simple example for Singleton Pattern This is a scenario where we keep a single instance(as in Singleton Pattern) to get the configuration of an application.

Below is the main class and i set values to the configuarion values here and call the methods of the other classes to check if they are getting the correct values.

public class A{
    public static void main(String []args){
        System.out.println("My Singleton example");
        System.out.println("Setting values to configs\n");
        configs conf = configs.getInstance();
        conf.setName("Home Page");
        conf.setUrl("www.google.com");

        B.showConfigValues();
        C.showConfigValues();

    }
}


Below is the singleton class.

public class configs{

    private static configs appConfig;
    private String name;
    private String url;

    private configs(){}

    public static configs getInstance(){
        if( appConfig == null){
            appConfig = new configs();       
        }
        return appConfig;
    }

    public void setUrl(String url){
        this.url = url;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getUrl(){
        return url;
    }

    public String getName(){
        return name;
    }
}


Below is class B and C. They access the configuration values in their methods.

public class B{

    public static void showConfigValues(){
        configs conf = configs.getInstance();
        System.out.println("Geting configs in Class B");
        System.out.println("url : "+conf.getUrl());
        System.out.println("name : "+conf.getName()+"\n");
    }
}

public class C{

    public static void showConfigValues(){
        configs conf = configs.getInstance();
        System.out.println("Geting configs in Class C");
        System.out.println("url : "+conf.getUrl());
        System.out.println("name : "+conf.getName()+"\n");
    }
}


Below is the output.

$ java A
My Singleton example
Setting values to configs

Geting configs in Class B
url : www.google.com
name : Home Page

Geting configs in Class C
url : www.google.com
name : Home Page


My question is, is this a good example to explain the singleton pattern?
For explaining the Singleton pattern the ABC naming is confusing. Just come up with some real name, e.g. Database, Logger etc. Something that might need access to a common config. Instead of System.out the config, you might want to add some mock code to init the database connection or get the logging directory.

Please keep in mind that Java classes usually are starting with a capital letter.

The static instance variable is usually called only instance.

In which context do you want to explain the pattern. Maybe we find a more suitable example?

Check http://www.oodesign.com/singleton-pattern.html for a nice explanation and some examples and drawbacks.

Please be aware that a Singleton might seem to help you sometimes in production code, but it definitely makes your life harder in testing your code. See "Dependency Injection" for an alternate approach.