Results 1 to 10 of 10
  1. #1
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default Why did Java designers make main method as static ?

    Dears,

    Does anyone know why Java designers made main method as static?
    Is there any special reason for that ? if so, Please let me know .
    Mak
    (Living @ Virtual World)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You can have nonstatic main methods if you want to:

    Java Code:
    import java.util.GregorianCalendar;
    class Xmas {
        private GregorianCalendar day;
        public Xmas(GregorianCalendar day) {
            this.day = day;
        }
    
        public void main(String[] args) {
            if(day.equals(new GregorianCalendar(2010, 11, 25))) {
                System.out.println("yay!");
            }
        }
    }

    But how would you make it do anything? I mean what command would you use at the command line to make this class do anything?

    I guess Java's designers opted to make the entry method for an application static, so they could answer such questions in a straightforward manner.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    How could a main method be non-static? A non-static method requires an instance of a class to call it with, and when entering a program, there are no instantiated objects to call methods on. It wouldn't make sense to have main methods non-static, now would it?
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You could have a non-static method if you figured that the program itself would be an object that could have calls upon it. Applets don't have a static main method (or a main method at all) because the Applet itself is an object.

    Honestly though a static method makes more sense. When a program is started, it engages a process, rather than an object.

  5. #5
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    You are right Mr.Singing Boyo

    We need object whenever we need to call non static method of the class.However we dont need objects for static class. The only need is "Name of object". We know that main method actually is called by JVM. So making main method as static would be sense.

    Most probably we got answer. However I am not gonna close this thread. I need to know what experts thinks.
    Mak
    (Living @ Virtual World)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    I need to know what experts thinks.
    No, you don't. You need to know what the language specification says about it.
    http://java.sun.com/docs/books/jls/t...on.html#12.1.4

    db

  7. #7
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    Darryl.Burke you are the expert ..
    Mak
    (Living @ Virtual World)

  8. #8
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Reason for using static

    We would like to call the main() method of the class without instantiation i.e. Creation of an object of the class.

    For a more detailed explanation Please visit:
    Total Gyaan: Why is the main method made static ?

  9. #9
    rohitjava is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default

    when u run the class, you use "java classname"
    u r runnning the class with name of class which inherently call the main method and JVM makes the object.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by rohitjava View Post
    when u run the class, you use "java classname"
    u r runnning the class with name of class which inherently call the main method and JVM makes the object.

    The JVM does not make any object in order for main() to be called.

    See the Total Gyaan link, or the JLS link, Singing Boyo's post. Or look at the example in my previous post of a nonstatic method called main() and try and answer the two questions that it posed. The constructor was deliberately chosen to make it incredible to expect an instance of that class to be created by the JVM. (If I had given it two constructors things would be even worse)
    Last edited by pbrockway2; 11-13-2010 at 10:57 PM.

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  2. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  3. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 04:17 AM
  4. make static ref to non-static method?
    By McChill in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 06:48 AM
  5. Replies: 5
    Last Post: 10-06-2008, 01: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
  •