ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Vivek Dudani , Software Engineer / Senior Software Engineer , Mastercard
تاريخ النشر: 2014/12/28
Majid Hameed
من قبل Majid Hameed , Senior Java Developer , Check24 Vergleichsportal GmbH

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

pardeep kumar
من قبل 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. 

المزيد من الأسئلة المماثلة