Java 在从子类构造函数调用之前,更改在超类中定义的静态变量

Java 在从子类构造函数调用之前,更改在超类中定义的静态变量,java,inheritance,constructor,Java,Inheritance,Constructor,我有一个带有静态PooledConnectionFactory变量的classA类别B扩展了A public class A{ private static PooledConnectionFactory con; public A(){ this("gets URL from defaults properties file"); } public A(final String url){ if(con==null){

我有一个带有静态
PooledConnectionFactory
变量的
classA
<代码>类别B扩展了A

public class A{
    private static PooledConnectionFactory con;
    public A(){
        this("gets URL from defaults properties file");
    }
    public A(final String url){
       if(con==null){
         //Initialize pooled connection       
       }
//code for connection here
    }
}
我想将PooledConnectionFactory作为一个非静态变量
。因为它是扩展的,所以调用转到A的构造函数


为了实现这一点,我应该在B类中做哪些更改?

使用单例而不是
A
构造函数

public class A {

    private static PooledConnectionFactory con;

    public A() {
        this("gets URL from defaults properties file");
    }

    public A(final String url) { }

    public static PooledConnectionFactory getPooledConnectionFactory() {
        if (con == null) {
            //Initialize pooled connection
        }
        return con;
    }
}

只需删除
静态
?我看不出有什么问题。你的问题很不清楚。是否要使
con
非静态?是什么阻止你这么做的?你到底想实现什么?我正在对现有产品进行更改,以确保新的更改不会影响原始流程。我在不同的类中进行了更改。两个
con
对象是否应该相同?在这种情况下,将
B
中的一个设置为受保护的
并从
A
@qomzwinx中删除
连接
,是的,但您到底想实现什么?是否希望
B
的不同实例使用不同的工厂,即使在超类方法中也是如此?如果不更改
A
,这是不可能的。
public class A {

    private static PooledConnectionFactory con;

    public A() {
        this("gets URL from defaults properties file");
    }

    public A(final String url) { }

    public static PooledConnectionFactory getPooledConnectionFactory() {
        if (con == null) {
            //Initialize pooled connection
        }
        return con;
    }
}