Wednesday, April 17, 2013

Java static class Vs. C# static class


While static fields and methods in Java and C# have the same function and meaning, static classes are different.

In C#, a static class may contain only static members, and cannot be instantiated into an object. It is also sealed - another class cannot inherit from it.

In Java, a static class is called a static member class. It must be declared inside another class. It is actually a regular class - it has none of the restrictions mentioned above for C# static classes. It is similar to a regular nested class in C#. So what's "static" about it? It can only reference static members of its containing class. This is in contradiction to a Java nonstatic member class, which automatically holds a reference to its containing class via the "this" keyword.

In Java, to make a static class behave more like a C# static class (namely to prevent its instantiation into an object), make its constructor private.

The writer is R&D team leader at Niloosoft Hunter HRMS