package com.xzit.util;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class Property {
private Property(){
}
private static final Property prop = new Property();

public static Property getInstance(){
return prop;
}


private InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");

private static final Properties loadFile(){
Properties props = new Properties();
try {
props.load(prop.is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return props;
}

public static final String getDRIVER(){
return loadFile().getProperty("driver");
}

public static final String getURL(){
return loadFile().getProperty("url");
}

public static final String getUSERNAME(){
return loadFile().getProperty("uname");
}

public static final String getPASSWORD(){
System.out.println("zz");
// System.out.println(loadFile().getProperty("pwd"));
// System.out.println("=============");
return loadFile().getProperty("pwd");
}

public static void main(String[] args) {
System.out.println(getPASSWORD());
System.out.println(getPASSWORD());
System.out.println(getPASSWORD());
}
}

解决方案 »

  1.   

    你写的逻辑很乱调用static 方法不需要单例
      

  2.   

    你的代码的确是实现了单例(饿汉模式),语法和逻辑都没有问题。
    不过正如1楼所说,你例子代码中根本没用上单例,你只是使用了静态方法,和单例无关。要使用单例,一定是通过getInstance方法获取类的唯一实例,然后通过实例方法来实现逻辑。
      

  3.   

    单例模式实现的没有问题,但一般单例模式喜欢用延迟加载,不过你所new的这个对象开销不大的话,这样写也行,更方便点。
      

  4.   

    package com.xzit.util;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
     
    public class Property {
        private Properties props = null;
        private Property(){
            loadFile();
        }
        public static final Property Instance = new Property();
         
        private InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
         
        private final void loadFile(){
            Properties props = new Properties();
            try {
                props.load(prop.is);
                this.props = props;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
         
        public final String getDRIVER(){
            return this.props.getProperty("driver");
        }
         
        public final String getURL(){
            return this.props.getProperty("url");
        }
         
        public final String getUSERNAME(){
            return this.props.getProperty("uname");
        }
         
        public final String getPASSWORD(){
            System.out.println("zz");
    //        System.out.println(loadFile().getProperty("pwd"));
    //        System.out.println("=============");
            return this.props.getProperty("pwd");
        }
         
        public static void main(String[] args) {
            System.out.println(Property.Instance.getPASSWORD());
            System.out.println(Property.Instance.getPASSWORD());
            System.out.println(Property.Instance.getPASSWORD());
        }
    }
      

  5.   

    Sorry,代码有点不完善,改一下
    package com.xzit.util;
      
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
      
    public class Property {
        private Properties props = null;
        private Property(){
            loadFile();
        }
        public static final Property Instance = new Property();
          
        
          
        private final void loadFile(){
            Properties props = new Properties();
            InputStream is = null;
            try {
                InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
                props.load(prop.is);
                this.props = props;
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(is != null) {
                    try{ is.close(); } catch(IOException e){ e.printStackTrace(); }
                }
            }
        }
          
        public final String getDRIVER(){
            return this.props.getProperty("driver");
        }
          
        public final String getURL(){
            return this.props.getProperty("url");
        }
          
        public final String getUSERNAME(){
            return this.props.getProperty("uname");
        }
          
        public final String getPASSWORD(){
            System.out.println("zz");
    //        System.out.println(loadFile().getProperty("pwd"));
    //        System.out.println("=============");
            return this.props.getProperty("pwd");
        }
          
        public static void main(String[] args) {
            System.out.println(Property.Instance.getPASSWORD());
            System.out.println(Property.Instance.getPASSWORD());
            System.out.println(Property.Instance.getPASSWORD());
        }
    }
      

  6.   

    public class OnlyOne {
    private static OnlyOne oo = null;
        /*private:禁止外界直接更改
         * static:只能有一个对象,所以刚开始必须用类名调用
         */
    private OnlyOne() {
    super();
    }
        //不允许外界直接初始化
    public static OnlyOne init() {
    if (oo == null)
    oo = new OnlyOne();
    return oo;
    }
    /*开放特殊的初始化方法
     */
    public static void main(String[] args) {
    OnlyOne ootest=OnlyOne.init();
    OnlyOne oo2=OnlyOne.init();
    System.out.println(ootest==oo2);
    System.out.println(ootest.equals(oo2));
    }}
      

  7.   

    如何正确地写出单例模式
    http://www.javafor.com/articles/2016/08/26/1472190589614.html