Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Vivek Dudani , Software Engineer / Senior Software Engineer , Mastercard
Date de publication: 2014/12/28
Majid Hameed
par Majid Hameed , Senior Java Developer , Check24 Vergleichsportal GmbH

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

pardeep kumar
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?