static 与私有构造函数
来源:互联网 发布:seo文章编辑 编辑:程序博客网 时间:2024/05/22 10:49
java中static是一个很有用的方法。
首先我们来看一个例子:
- package com.test.dao;
- public class Test {
- private static int i = 0;
- private Test() { // ************************* 1
- }
- static {
- System.out.println(i++);
- }
- public static int getI() {
- return i;
- }
- }
- package com.test.dao;
- public class Rua implements Runnable {
- public void run() {
- System.out.println("RUN: " + Test.getI());
- }
- public static void main(String args[])
- {
- Thread r1=new Thread(new Rua());
- Thread r2=new Thread(new Rua());
- Thread r3=new Thread(new Rua());
- r1.start();
- r2.start();
- r3.start();
- }
- }
首先我们来分析Test这个类,这个类中,有一个私有的构造函数,并且有一个static 的方法,在Rua这个类中,到底会有什么样的打印结果?
这个是得到的结果:
为了更好的说明这一点,继续看下面的两个类:
- public class Test {
- private static int i = 0;
- private Test() {
- }
- static {
- System.out.println(i++);
- }
- public static int getI() {
- return i;
- }
- }
- package com.test.dao;
- public class Rua implements Runnable {
- public void run() {
- System.out.println("RUN: " + Test.getI());
- }
- public static void main(String args[])
- {
- System.out.println(Test.getI()==Test.getI());
- }
- }
打印出的内容为:
这说明两次所得到的实例是相同的。即单例模式。
所以,在这里不难看出,在static方法内,只运行了一次,并且形成了三个单例,每个单例之间都相互不影响,在Test类中,分别构造了三个不同的实例,这三个实例间相互不会影响。
这里还有一个问题,注意看注释1,这里没有使用共有构造函数,而是使用了私有构造函数,那么如果在这里使用了共有构造函数会有什么影响呢?首先,使用自由构造函数,这个类不可以初始化,即不可以被new出来,如果没有显示的声明构造函数,那么系统会为默认的使用该类的共有构造函数,因此在这里,既然需要使用到单例,那么在这不可避免的就需要使用到私有构造函数,因为,如果使用了共有构造函数,那么可能造成的影响:该类可以被new出来,这样的话,就不能被保证该实例会被这样而使用。
这里还有一点可能会发生疑问的就是,如果构造函数和static同时存在的时候,系统到底先执行哪一个呢?
系统首先会运行static内部的内容。
这里能够引出的问题:大多数人应该看过hibernate的源码,在这里贴一段过来
- public class HibernateSessionFactory {
- /**
- * Location of hibernate.cfg.xml file.
- * Location should be on the classpath as Hibernate uses
- * #resourceAsStream style lookup for its configuration file.
- * The default classpath location of the hibernate config file is
- * in the default package. Use #setConfigFile() to update
- * the location of the configuration file for the current session.
- */
- private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
- private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
- private static Configuration configuration = new Configuration();
- private static 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("%%%% Error Creating SessionFactory %%%%");
- e.printStackTrace();
- }
- }
- private HibernateSessionFactory() {
- }
- /**
- * Returns the ThreadLocal Session instance. Lazy initialize
- * the <code>SessionFactory</code> if needed.
- *
- * @return Session
- * @throws HibernateException
- */
- public static Session getSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- if (session == null || !session.isOpen()) {
- if (sessionFactory == null) {
- rebuildSessionFactory();
- }
- session = (sessionFactory != null) ? sessionFactory.openSession()
- : null;
- threadLocal.set(session);
- }
- return session;
- }
在这里,我们便可以看出点hibernate对于数据库连接的眉目了
下面看看Static在hibernate中的一个应用。
package com.nvidia.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
//成员变量的创建,
private static SessionFactory factory;
//只装载一次变量的写法
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch(Exception e){
e.printStackTrace();
}
}
public Session getSession(){
return factory.openSession();
}
public void closeSession(Session session){
if(session != null)
if(session.isOpen())
session.close();
}
public static SessionFactory getSessionFactory(){
return factory;
}
}
- static 与私有构造函数
- static 与私有构造函数
- 私有构造函数与私有析构函数
- 非public构造函数与私有继承
- 私有构造函数
- 私有构造函数
- 私有构造函数
- 私有构造函数
- 私有构造函数
- 私有的构造函数
- 私有构造函数
- C#私有构造函数
- 私有构造函数用途
- C++私有构造函数
- 私有构造函数
- 私有构造函数
- 私有构造函数
- 黑马程序员---构造函数与Static静态
- “求二进制数中1的个数”
- 设计模式一(Factory Method,Abstract Factory)
- Calling startActivity() from outside of an Activity context requires the FLA
- 在ErlyWeb中处理中文字符
- 音视频设备支持Onvif
- static 与私有构造函数
- JMeter进行WebService测试
- LINUX系统学习系列---备忘录
- HDU-1269(双DFS求强连通性)
- WIP Supply Type(Push,Assembly pull,Operation pull...)
- Android调用WebService
- COM
- Servlet3.0的新特性
- Silverlight实现强壮、可复用的拖放行为