• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why can't a method be inside the main public static main method?

 
Greenhorn
Posts: 4
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do methods have to be outside the public static main method? In the code below, I kept getting the following error
illegal start of expression
void talk(){

when I compiled the program. I had to comment out the code and place a copy of the method
outside the public static void main method as shown below. Then it ran ok.


class Speak{

public static void main(String[] args){
Speak one = new Speak();
one.talk();

//void talk(){
//System.out.println("hello");
//}


} // end of main method


void talk(){
System.out.println("hello");
}


} // end of Speak class
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is that it is illegal because it does not have a defined meaning.

Methods cannot be defined within other methods because there is no language rubric for them to be put within another method. There is no "scope" for a method definition that makes sense within another method; you cannot "dereference" a method to invoke a method defined within it (main().talk() means something else).

The question is really more why you think this construction is legal, or just what you think it means. You can't put a method definition there because the compiler doesn't know what you mean by that, and neither do the rest of us.

rc
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
languages have EXTREMELY specific definitions of what is allowed and what is not. The Java Language Specification are those rules for this language, and are available for anyone to read. You can even write your own compiler, if you like.

Those rules simply do not allow for you to have a method defined inside another method. The creators felt there was no reason to do so, an attempting it would cause more problems (for the compiler writers) than it was worth.

What do you feel you could gain by doing it that you can't get by not nesting methods?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic