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 ?
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.
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.
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.
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.
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());
}
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With