简体   繁体   中英

Checking if a final instance variable has been initialized

I am still learning Java but I am not sure why this is causing compile time error:

public class AnswerRepository implements IAnswerRepository
{
    private final static SQLiteDatabase database;

    public AnswerRepository(Activity activity)
    {
        if(database != null)
        {
            database = DbOpenHelper.getInstance(activity);
        }
    }
}

I am just trying to check if a final variable has been assigned first before assigning a value to it. But it seems compile time checking doesn't like it. Why is that?

在此输入图像描述

final variables can only be initialized once. Normally, they must be initialized in the constructor, but if they are static , then they need to be initialized when they are defined, like this:

private final static SQLiteDatabase database = new SQLiteDatabase(...);

or, you can initialize it later:

private static SQLiteDatabase database;

static variables will be initialized before object constructors are called. So in this case, database will always be null and since it is null , re-initializing it in the object constructor will cause a compile time error.

You can't initialize final static variable on constructor. To initialize use inline statement,

private final static SQLiteDatabase database= DbOpenHelper.getInstance(activity);

or, use static block.

 private final static SQLiteDatabase database;

 static{
       database = DbOpenHelper.getInstance(activity);
 }

Initalize database variable.

private final static SQLiteDatabase database = null;

FYI : You should initialize a static final variable either in a static initializer, or directly. So either

static final SQLiteDatabase database = null;

Or

static final JButton button;

static {
  button = new JButton();
}

Read this doc for more info.

At this point:

if (database != null)

It's impossible that database has a non-null value: you didn't initialize it at the declaration, nor at any static initialization block. So the compiler is correctly complaining that the variable is null at this point.

Either initialize it at the declaration:

private final static SQLiteDatabase database = ...;

Or use a static initialization block:

static {
    database = ...;
}

Final static member must be initialized in static way. In your case. You need remove final keyword.

Static variables are belong to Class not to any instances of that class. And they will be initialized in class loading time. So, initializing it in the constructor doesn't make any sense. Since for every instance creation, that will be reinitialized. This may cause problems to you. Use static initialization block to initialize them.

Final variable either be null initially & then when it has reference then it wont change. so you have to put

database = null;

or else you have to assign some value out side of if condition.

so if if does not execute then also it have to assign value.

static members must be intialized in a

static { }

block

  • also you are using an IDE most IDE's have strict code error setting that are different than compilers so you might need to check that as well if above doesn't resolve it *

Since you are trying to create instance of database variable in AnswerRepository constructor, I don't see any necessity why there is a null check in the constructor.

This should have been handled in DbOpenHelper.getInstance(activity) not here.

Your code can be like the below one and will serve your purpose.

public class DbOpenHelper
{
   private static SQLiteDatabase database;

  public static SQLiteDatabase getInstance(Activity activity)
  {
      if( null == database )
      {
          database = new SQLiteDatabase(...);
      }
      return database;
  }
}

There are many good ways of creating singleton. Choose a good one. The one that is posted above is just for an example.

As mentioned by others, but the below code in static block

database = DbOpenHelper.getInstance(activity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM