C# vs. Java: "Static" Classes
I came across a little problem recently when I tried to write a class in Java as static. Nesting is the closest way I could find to do this. With C# I have been using the static keyword on the class, which is quite slick and makes the code easy to read.
Java nested static class
public class OuterClass
{
/* Throw in some methods and stuff here... */
public static class InnterClass
{
public static void WhateverMethod ()
{
/* If instance methods added here, possible ouch? */
}
}
}
C# static class
public static class Widget
{
/* Ain't havin' no instance members here no how! */
}
OK so this may seem nitpicky but there was actually a bug that snuck into the .NET 1.1 Framework where the static modifier was accidentally left off one of the System.Environment methods. So in C# if you add the static keyword to your class the compilier will not allow constructors, destructors, instance methods or instance variables. That ought to keep you out of trouble.
AFAIK this syntax simplicity does not exist in Java. The nested inner static class is the closest thing I've found. If anyone knows of a less verbose Java way to do this please let me know.