You're defining your function "calcArea" inside the definition of "main()". You can't do that -- you can't define one function inside of another one. Move the definition of "calcArea()" out to the class level -- i.e., before the "public static void main()" line:
Rajesh Shekar wrote:Also i assume if i move the method outside main i need to create an object for the class "MethodTest" and call the calcArea method is that right?
Not if you declare the method to be static (which it should be in the case you listed above). A static method from within the same class can be called by its name alone, i.e. calcArea(w, h). If you are calling it from another class and if the function has the appropriate access modifier (eg: public), then you should call the static method by ClassName.staticmethodname(), i.e., MethodTest.calcArea(w, h). Note that this method call is not dependent on any instance of the MethodTest class.
Not convinced. It is a bad idea to make methods static until you really know what static means. It would be better to create a Square object, and call its calcArea() methodYou will obviously have to work out how to create your Rectangle class, with a constructor and fields.
Joined: Feb 25, 2012
Thanks a lot i understood many things from this
1) Name a class properly.
2) use constructor with parameters (yet to learn this)
3) need to learn static.