Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to static field from instance method

Tags:

java

sonarqube

I have my code as below. I see

public MyClass{

    private static DataSource dataSource = null;

    private static DataSource getDataSource(){
        if (dataSource == null) {
            try {
                dataSource = // something.
            } catch (Exception e) {
                // some exception.
            }
        }

        return dataSource;
    }

    public List doSomething(){

        // ...

        if(dataSource == null){
            dataSource = getDataSource();
        }

        dataSource.getConnection();
        // ...

    }
}

I see following message in sonar anaylsis.

Dodgy - Write to static field from instance method

This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice.
findbugs:ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD Sep12 Reliability > Architecture

I see everything is okay in this implementation except that we are changing the static variable in doSomething method. How do we fix this ?

like image 284
TechCrunch Avatar asked Jul 11 '14 17:07

TechCrunch


People also ask

Can you make a static reference to an instance field?

data); i.e. referring a variable using static reference implies to referring using the class name. But, to access instance variables it is a must to create an object, these are not available in the memory, before instantiation. Therefore, you cannot make static reference to non-static fields(variables) in Java.

Can instance methods assign values to static variables?

Instance method can access the instance methods and instance variables directly. Instance method can access static variables and static methods directly. Static methods can access the static variables and static methods directly.

Can we call static method from instance method?

So a static method can call an instance method as long as it has a reference to an instance to call it on. Show activity on this post. Static methods can be called freely, but instance methods can only be called if you have an instance of the class.

Can static method use instance attribute?

Instance & Static Attribute : You declare static methods using the CLASS-METHODS statement. if u declare one attribute as a instance then we can use that attribute through object name, that attribute is dependent of that object. You declare instance methods using the METHODS statement.


2 Answers

Not sure how your static analysis tool works but -

try writing to your value via a static setter:

private synchronized static void setDataSource(DataSource ds) {
    dataSource = ds;
}

so that you can do

   if(dataSource == null){
        setDataSource(getDataSource());
   }
like image 139
David T. Avatar answered Oct 13 '22 00:10

David T.


You have to choose between two solutions , first one is in your Datasource class add getDatasource() method and synchronize the block where you instantiate your static field :

    private static DataSource getDataSource(){
     synchronize(DataSource.class){
            if (dataSource == null) {
                try {
                    dataSource = // something.
                } catch (Exception e) {
                    // some exception.
                }
            }
       }
        return dataSource;
      }

then you will just call this method from your doSomething() method

public void doSomething(){
//
DataSource dataSource=DataSource.getDataSource();
//
}

second solution is to instantiate your field at the class loading time using a static block or directly from the declaration.

static {
 dataSource = // something.
}
like image 27
Mifmif Avatar answered Oct 13 '22 00:10

Mifmif