Results 1 to 6 of 6
  1. #1
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Question [SOLVED] Why main() in java is declared as public static void main?

    Why main() in java is declared as public static void main?

    This question was asked to my friend in her interview.

    I am looking for a well organized answer on this question, which can be used in an interview, can someone help.
    Live life king size
    [Lucene]

  2. #2
    kzvi.kzvi.1 is offline Member
    Join Date
    Oct 2008
    Location
    US
    Posts
    58
    Rep Power
    0

    Default

    Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application

    static - When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class.

    void - Java is platform independant language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system.

    Read more about Java Fundamental Questions : Java Fundamental Questions

    ____________________________________________
    Software Wiki
    Have fun....
    JAVA FAQs

  3. #3
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    I would advise to Search First, but in this case the search words form a bit of a problem: every post with code with a main method will turn up in the results.

    Still, this question has been asked before, so perhaps if you can find the right words to sort on you might get a result with the answer.

    ____________________________________________
    Cooking is Fun

  4. #4
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    The forum has lots of similar questions asked before, you are not the first one to face this. So make sure that you are utilizing the forums potential to fullest.
    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    Well to do that, we may have to have some idea what book the interviewer has read and believes. Basically, it is because someone decided to do it that way but that will not go over in an interview. My suggestion is to do direct research on the JSR's and if there is no statement on the matter in the Java Specification - then that is the true answer but additional work would be needful to have several answers ready, trying to get them all out before interviewer plays one-ups-man-ship on would be coder.

    No matter how you word your reply, there will be some snip-nosed replies from people who fabricated interview questions rather than writing code.

    6-months filppin burgers will make such questions nubile in nature.

    { message edit: I go with kzvi.kzvi.1 - as this appears to be the correct answer }
    Last edited by Nicholas Jordan; 10-05-2008 at 11:22 PM. Reason: correct answer already posted
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. Replies: 5
    Last Post: 01-24-2008, 11:53 PM
  3. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  4. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Posting Permissions

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