Declare a constant variable in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Declare a constant variable in java

Hi guys please help me My task is to declare a constant variable COURSE with value PROG1 in the class. The constant should be visibel outside the class. public class AClass{// this is fixed i guess static final String COURSE= ”PROG1” Public String toString () {//this is fixed I guess Return COURSE; } } But i get nosuchfieldexception :COURSE It expected true test Try { java.lang.reflexer.Field f= AClass.getfield(COURSE ”); Modifier is final something

10th Jan 2023, 2:10 AM
Melissa 123
Melissa 123 - avatar
4 Answers
+ 2
declare it public? public static final String COURSE = "PROG1";
10th Jan 2023, 2:53 AM
Bob_Li
Bob_Li - avatar
0
I tried it didn’t help
10th Jan 2023, 2:55 AM
Melissa 123
Melissa 123 - avatar
0
full code? How are you testing it?
10th Jan 2023, 2:57 AM
Bob_Li
Bob_Li - avatar
0
ok, I didn't notice it, but public and return should not be capitalized. public class Program { public static void main(String[] args){ AClass a = new AClass(); System.out.println(a.toString()); System.out.println(AClass.COURSE); } } public class AClass{ public static final String COURSE= "PROG1"; public String toString(){ return COURSE; } }
10th Jan 2023, 7:00 AM
Bob_Li
Bob_Li - avatar