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
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 56230
    
  13

Because a method cannot be defined within another method.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 60
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
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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 7 API documentation
Scala Notes - My blog about Scala
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: 4095
    
    1
praise the lord!


SCJP
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: why not we define a static member function inside a main
 
Similar Threads
please help me to corect the error
getting instance of a class
Regex - Java or Perl
I FACE SOME ERROR, PLEASE HELP ME, ALSO I SEE THE RESULT IN SIMPLE GUI
error in prepared statement