aspose file tools*
The moose likes Beginning Java and the fly likes Get more error in the below program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get more error in the below program" Watch "Get more error in the below program" New topic
Author

Get more error in the below program

Rajesh Shekar
Greenhorn

Joined: Feb 25, 2012
Posts: 8
Hi All,
Im very new to java and i wrote a program as below and i get 4 errors not sure what they mean can someone help me?


MethodTest.java:6: ';' expected
int calcArea(int height,int width)
^
MethodTest.java:6: <identifier> expected
int calcArea(int height,int width)
^
MethodTest.java:6: not a statement
int calcArea(int height,int width)
^
MethodTest.java:6: ';' expected
int calcArea(int height,int width)
^
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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:



[Jess in Action][AskingGoodQuestions]
Rajesh Shekar
Greenhorn

Joined: Feb 25, 2012
Posts: 8
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?
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
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.
Rajesh Shekar
Greenhorn

Joined: Feb 25, 2012
Posts: 8
Hi Both,
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.

Thanks a lot for helping me in this one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get more error in the below program