Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not we define a static member function inside a main

 
abhinav sood
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class emp
{
static String cname="Abhinav";
public static void main(String... s)
{
static void display()
{
System.out.println(cname)
}
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a method cannot be defined within another method.
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the exception of methods from Anonimous classes of course. Tecnically are inside a methods
 
Badal Chowdhary
Ranch Hand
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhinav, the question is what are you trying to achieve here?
 
abhinav sood
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to know how the reference variable works if a method contain nested methods.......
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Armenteros wrote:With the exception of methods from Anonimous classes of course. Tecnically are inside a methods

You can define classes inside methods (not just anonymous classes). This is valid Java, for example:

But you cannot define methods directly inside methods.
 
Badal Chowdhary
Ranch Hand
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, methods cannot be nested in methods. A method can only be inside a class.
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praise the lord!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic