Saurabh Ghorpade Saurabh Ghorpade - 5 months ago 30
Java Question

C# sealed vs Java final

Would anybody please tell me as the reason the following use of

does not compile? Whereas, if I replace
and compile it as Java, it works.

private sealed int compInt = 100;
public bool check(int someInt)
if (someInt > compInt)
return true;
return false;


That's because final in Java means plenty of different things depending on where you use it whereas sealed in C# applies only to classes.

In Java final can be applied to:

  • classes, which means that the class cannot be inherited. This is the equivalent of C#'s sealed.
  • methods, which means that the method cannot be overridden in a derived class. This is the default in C#, unless you declare a method as virtual and in a derived class this can be prevented for further derived classes with sealed again.
  • fields and variables, which means that they can only be initialized once. For fields the equivalent in C# is readonly.