Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree1Likes

Thread: main method String

  1. #1
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default main method String

    class sample
    {
    public static void main()
    {
    System.out.println("Hai");
    }
    }
    if we compile the above program it doesnot shows any error msgs.. But you run the above program it shows an error msg as java.lang.NoSuchClassFound...... why?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: main method String

    you are missing the argument in your main

    Java Code:
    public class Sample {
    
        public static void main(String[] args) {
            System.out.println("Hai");
        }
    }
    make that change, recompile and try it again.
    Last edited by sehudson; 09-19-2011 at 07:20 PM.

  3. #3
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    Sir, Idont want create a pakage.... Iam asking why the java interpreter sends an error msg while running the above program....

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: main method String

    ignore the package name. just add the String[] args inside the main parenthesis.

  5. #5
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    Sir, that's iam saying,
    If we write string args[] in main it gives the o/p. but if we dont write the program complied successfully.. But while running that program it shows an error msg. why?

  6. #6
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: main method String

    Java interpreter sends the message because it is looking for the method with the signature public static void main(String[] a)
    and you have not written that. Secondly, you are telling that you are getting the error java.lang.NoSuchClass error which i think you should not get and you
    will get java.lang.NoSuchMethodError

  7. #7
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    Thank you Rameshwar.
    It means it's essential thing to write string args[] in main... without writing this we cannot run this right?
    But in c/cpp we dont declare any variable..... why it's essential to write in java?

  8. #8
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    if i dont want to parse the values to main, so in this case it's not essential to write string args[] in main right?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: main method String

    The java program only looks for a main(String[] ) method as the starting point to the program.
    Ask the author of the java program why he only wanted that one entry point.
    Its not important enough to argue about. It works. use it.
    PhQ likes this.

  10. #10
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    try this:
    class sample
    {
    static
    {
    sample.main();
    System.exit(0);
    }
    public static void main()
    {
    System.out.println("Hai");
    }
    }
    In the above program the main method dint recive any values. and you run this above progrm you get the o/p. why?

  11. #11
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: main method String

    This is what the Java creators had decided. It should be there means it should be there and we have to follow certain rules.
    Why 2+2=4 and not 5. I don't know but it is said 4 and it is 4. Anyways
    Quote Originally Posted by chandrasekhar.melam View Post
    why it's essential to write in java?
    See it is essential in Java because what would you do if you want to pass certain arguments at run-time? So what ever you pass at run-time is stored in String array
    which you declare in main method. I think an example is best to explain
    Java Code:
    class Demo
    { 
       public static void main(String[] args)
       {
            int ans=(Integer.parseInt(args[0])) +(Integer.parseInt(args[2]));
    	System.out.println(ans)	;
        }
    }
    you will run this program java Demo 2 3 and the output is 5.
    So you can see how that array in main method was used.

  12. #12
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    ya i know this one. if i dont send the command line arguments, then?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: main method String

    In your code sample you could have called the method anything. It didn't have to be main.
    Java Code:
    class Sample {
      static {
         Sample.startHere();
         System.exit(0);
      }
    
      public static void startHere() {
        System.out.println("Hai");
      }
    }

  14. #14
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    ya i know. bcz the static is block which was called before the main method called, right.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: main method String

    The main method in your code would not be called by the java program. It's Your code that calls it and it could be with any name as far as java is concerned.

  16. #16
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    is main method is predefined or userdefined method?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: main method String

    Please explain what is predefined and what is userdefined.

  18. #18
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    both(pre and user defined datatype).
    Bcz the main method was created by user. that's way we called it as user defined. and
    every java program excution was starts with main method. it's predefined. so that's way we called predfined. Right?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: main method String

    the main method was created by user. that's way we called it as user defined.
    That is correct. I type in/create all of the code in my programs.

    every java program excution ... starts execution with main method. it's predefined.
    That is correct. The java program calls a public static main(String[]) method.

  20. #20
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: main method String

    thank for giving some nice answers. but the confusion was stilll going. ok

Page 1 of 2 12 LastLast

Similar Threads

  1. Main Method
    By maya700 in forum New To Java
    Replies: 13
    Last Post: 07-23-2011, 08:41 AM
  2. main method
    By 007 in forum New To Java
    Replies: 18
    Last Post: 06-25-2011, 01:47 PM
  3. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  4. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  5. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •