The short answer is that you have to put Java statements inside methods -- that's just the rule.
The slightly longer answer is that a variable declaration with or without an initializer isn't a statement, it's a declaration, and those can appear at class scope; but all other code must be inside a method or block.
In the second case, when should the code be executed?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
You could think of it in this way: A class describes the structure of the object and its functionality. Structure is described by member variables. eg. String item="airplane"; Functionality is described as methods. e.g. System.out.println("Zoooooom awaaaaaaay"); Hence System.out, which describes the behaviour of the object, goes in a method. [ October 05, 2004: Message edited by: suhaasi karnik ]
Hi all, That's true, in java, Other than the variable declarations, all the code has to be within the body of methods. because in java the communication happens only through methods of a class. If some code is outside a method then that gives a compilation error. [ October 05, 2004: Message edited by: prashant bhogvan ]
Impossible is I M Possible
Joined: Jul 11, 2001
Originally posted by prashant bhogvan: Hi all, That's true, in java, Other than the variable declarations, all the code has to be within the body of methods.
That's not fully true: constructors aren't nethods; and you can have static and instance initializers.