Java only allows limited code outside of initialiers, constructors, and methods. The only code is the initialization of class and instance variables. The first case violates this. It is not in an initializer, constructor, method, or declaration of a class or instance variable.
The second case is fine because it is in a method.
Hi Henry, Can you please explain more about your expalanation. In Standalone program where can we write public void init() method. How this method can be called, or JVM will by itself calls this method.
"The second case is fine because it is in a method." First method is also in a method.