aspose file tools*
The moose likes Beginning Java and the fly likes why not we define a static member function inside a main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why not we define a static member function inside a main" Watch "why not we define a static member function inside a main" New topic
Author

why not we define a static member function inside a main

abhinav sood
Greenhorn

Joined: Aug 05, 2011
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61648
    
  67

Because a method cannot be defined within another method.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 73
With the exception of methods from Anonimous classes of course. Tecnically are inside a methods
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Abhinav, the question is what are you trying to achieve here?


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
abhinav sood
Greenhorn

Joined: Aug 05, 2011
Posts: 9
I wanted to know how the reference variable works if a method contain nested methods.......
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Basically, methods cannot be nested in methods. A method can only be inside a class.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

praise the lord!


SCJP
Visit my download page
 
 
subject: why not we define a static member function inside a main