Can we initialise final variable using constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can we initialise final variable using constructor

validation of final keyword

25th Oct 2017, 7:07 AM
Saiyam
Saiyam - avatar
1 Answer
+ 5
Yes, once final member is set it cannot be changed though. class Test { private final int NUM; Test(int num) { this.NUM = num; } public int getNum() { return NUM; } } public class Program { public static void main(String[] args) { Test t = new Test(5); System.out.println(t.getNum()); } }
25th Oct 2017, 7:23 AM
ChaoticDawg
ChaoticDawg - avatar