learn java
The moose likes Beginning Java and the fly likes getting ; expected error for the below program.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting ; expected error for the below program.." Watch "getting ; expected error for the below program.." New topic

getting ; expected error for the below program..

Janardhan Mudduluru

Joined: Jul 09, 2009
Posts: 4
Hi Mates,

I am a beginner...getting ; expected error for the below program..

solution will be much appreciated..

Janardhan M
Henry Wong

Joined: Sep 28, 2004
Posts: 19528

You can't just declare a method anywhere. In this case, you are trying to declare the hi() method inside the main() method. This is not legal.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

let me modify your example so that it works...

There you go...

what you should learn:
1. methods are not functions, so you cant have:

you are permitted:

2. methods are not functions , so you cant have

you are permitted:

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Av Vinay

Joined: Oct 27, 2009
Posts: 6

public class Example {

public Example() {
public static void main(String[] args){
String c = hi();
public static String hi()
return "Hi";

As explained in the last reply., You just can't write the metod definintion anywhere you want. The other solution for your problem is to write a static function which can be directly called by the static function main() as shown above.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41589
Your code is hard to read if you don't use the Code button.
Have you tried LearnNowOnline? http://www.learnnowonline.com/
subject: getting ; expected error for the below program..