Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: method inside main()

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default method inside main()

    Hi,
    In structured programming language like c we could create a method in main().In java its not possible.Why? Or is there any way we can create a method in main using keywords like static or something of that sort.
    class A{
    public static void main(String args[]){
    System.out.println("In main()  ");
     
    met();
     
    void met(){
    System.out.println("In met()   ");
     
    }
     
    }
     
    }

    Output:

    In main()
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: met
    at A.main().java:5

    BUILD SUCCESSFUL (total time: 3 seconds)


    Yes i know if we place the met() method outside main() and within class A scope then it will run.But cant we do it like a simple c program with methods inside main.I don't need to know what can be the use of it.But can we do it in any other way or not?Let
    me know if there is any possible way.

    Thanks in advance
    Last edited by helloworld922; July 14th, 2011 at 11:31 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: method inside main()

    Please use the code tags or highlight tags. Also, please properly format your code. It's much easier to read when it has the proper spacings and tabbings.

    I've never heard of being able to define a function inside of another function in C or C++. There are closures/lambda functions in the new C++0x language specifications, but I would consider those to operate differently.

    You can declare function prototypes inside of another method, but personally I don't see any practical applications of that (typically you want to declare function prototypes either in an external header file, or at the top of your source file).

    Since Java does not require you to declare a method "above" the point where you use it, there is no need to declare a method prototype.

    // the function prototype is necessary in C/C++ to tell the compiler that such a method exists
    void doIt(void);
     
    int main(void)
    {
         doIt();
    }
     
    // ... doIt() is defined somewhere below main in the source, but is NOT inside of another function
    void doIt(void)
    {
        std::cout << "doIt" << std::endl;
    }

    alternatively, simply switch the order of the function declarations/definitions and you won't need any prototype at all.

    // no function prototype is required since doIt is declared and defined before it's used. This won't work if there's a mutual dependency, you'll need to use function prototypes if there are mutual dependencies.
    // again, doIt's definition is not inside of any other function
    void doIt(void)
    {
        std::cout << "doIt" << std::endl;
    }
     
    int main(void)
    {
         doIt();
    }

    This same idea is instantly captured in Java because the ordering of method declarations/definitions is not important.
    Last edited by helloworld922; July 14th, 2011 at 11:54 PM.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: method inside main()

    Quote Originally Posted by rohan22 View Post
    But cant we do it like a simple c program with methods inside main
    Of course you can, you just need to compile your code. Oh wait, no you can't! I guess you cannot have a method inside another method then. The reason Java cannot do what C can is because it is Java not C. If you want a programming language that does things like C then use C.
    Improving the world one idiot at a time!

Similar Threads

  1. Java main method args
    By mattxo in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 09:45 AM
  2. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  3. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM
  4. how to execute a simple display without a main method
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 13th, 2010, 06:28 AM
  5. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM