1
public class ClassWithInnerClass {

    int a = 10;

    public static void outer(){
        System.out.println("In method of outer class");

    }


        final static class Inner{

            int b = 20;

            public void innermethod(){
                System.out.println("In method of inner class");
                System.out.println("Inner class variable b = "+b);

            }
        }
}

In the above code, i have an outer class and then there is a static nested class with the non-access specifier 'final'.Does this make this class similar to "Constant" variables?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

2 Answers2

0

No, it's not similar to constant variable. It means that you can't create a sub-class of this nested class (Inner). This is similar to using the final keyword in top level (i.e. not nested) classes.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

final on classes mean that they cannot be extended. It's no different from having final on a class defined at the top level.

final on classes is not the same as final on variables. final has different meanings based on where you use it. In general it describes a sort of immutability:

  • On variables, it means that you cannot change its value once initialized.
  • On methods, it means that once defined, it cannot be overridden in a subclass.
  • On classes, it means that the class itself cannot be subclassed.
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295