Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Can we declare an instance variable as final without initializing it at the time of declaration? If yes, how?

user-image
Question added by Vivek Dudani , Software Engineer / Senior Software Engineer , Mastercard
Date Posted: 2014/12/28
Majid Hameed
by Majid Hameed , Senior Java Developer , Check24 Vergleichsportal GmbH

Yes you can. But you need to initialize that instance variable in constructor.

pardeep kumar
by pardeep kumar , Senior R & D Engineer , Mavenir Systems

@Majid is right. Just to add more on this

A final variable can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases. 

More Questions Like This