天天看點

java中的static關鍵字用法

static方法

在靜态方法中不能通路非靜态成員方法和非靜态成員變量,但是在非靜态成員方法中是可以通路靜态成員方法/變量的。

static方法

可以在沒有建立任何對象的前提下,僅僅通過類本身來調用static方法。

例如

下面是一個靜态方法

public static Object getBean(String beanName){
        Object bean = null;
        try {
            String beanPath = props.getProperty(beanName);
//            System.out.println(beanPath);
            //用反射的方式
            bean = Class.forName(beanPath).newInstance();//每次都會調用預設構造函數建立對象
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }      
private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao");      

靜态代碼塊

繼續閱讀