为何这个SessionFactory不能同时使用static final修饰,不能编译

j2ee_yohn 2009-02-20
public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/com/yohn/hib/hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static final org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% 7777777777 Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateSessionFactory() {
    }
}
HeroXuan 2009-03-23
[b][/b] 
HeroXuan 2009-03-23
j2ee_yohn 写道
public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/com/yohn/hib/hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static final org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% 7777777777 Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateSessionFactory() {
    }
}

sdfdsf
cgjcsl 2009-03-29
老兄,我也刚接触不久.我是看不出问题,感觉没错。
yhsniaw 2009-03-29
final 修饰的变量为常量,必须先赋值,并且程序中不能改变其值
如:
private static final SessionFactory sessionFactory = configuration.buildSessionFactory();
j2ee_yohn 2009-03-30
yhsniaw 写道
final 修饰的变量为常量,必须先赋值,并且程序中不能改变其值
如:
private static final SessionFactory sessionFactory = configuration.buildSessionFactory();

我试了别的对象,可以这么做!
danielli007 2009-05-04
final类型的实例变量在使用类静态块初始化的时候,不允许使用try-catch,但可以使用try-finally
jsjzhou 2010-04-26
public class HibernateUtil {
/** sessionFactory */
private static final SessionFactory sessionFactory;

static {
sessionFactory = new AnnotationConfiguration().configure(
"org/jianhua/hibernate/lesson04/util/myhibernate.cfg.xml")
.buildSessionFactory();
}

/**
* 获取session
*
* @return
*/
public static Session getSession() {
return sessionFactory.openSession();
}
}
jsjzhou 2010-04-26
可以用static final的。
552jun 2011-11-12


2
Global site tag (gtag.js) - Google Analytics