I am doing an exercise about Java class. The code is as follows.
I can run the code without problem if I add a static modifier before the embedded call Explorer. However, if I do not add the "static", I would see the following error:
Illegal static declaration in inner class Government.Explorer modifier 'static' is only allowed in constant variable declarations.
BTW, the exercise is from CS61B spring 2018 from UC Berkeley. Original code is provided here:
public class Government {
private int treasury = 5;
public static Government greaterTreasury(Government a, Government b) {
if (a.treasury > b.treasury) {
return a;
}
return b;
}
public static class Explorer {
public static void doStuff(Government a, Government b) {
Government favorite = Government.greaterTreasury(a, b);
System.out.println("The best government has treasury " + favorite.treasury);
}
}
public static void main(String[] args){
Government a = new Government();
a.treasury = 10;
Government b = new Government();
b.treasury = 11;
Government.Explorer.doStuff(a, b);
}
}