Search code examples
javafinal

the blank final field may not have been initialized


PS: There are questions with similar topic but what I am asking is essentially different.

I understand the reason behind the error

the blank final field may not have been initialized

when I try to do something like

  final Object[] items;

But what concerns me is the JDK ArrayBlockingQueue implementation HERE. How is that same line (line #87) is getting used here with no errors ?


Solution

  • It is because the fields are initialized in the constructor

      public ArrayBlockingQueue(int capacity, boolean fair) {
            if (capacity <= 0)
                throw new IllegalArgumentException();
            this.items = new Object[capacity];
            lock = new ReentrantLock(fair);
            notEmpty = lock.newCondition();
            notFull =  lock.newCondition();
        }