Results 1 to 10 of 10
  1. #1
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default Newbie question about Static methods

    Hello everyone... need to ask u guys a question.

    Why do we need the word static in the declaration of the main method ???
    Now I see that the main method has to be public so the JVM can see it from outside and I KNOW that static means that its not necessary to create an instance of the method!. But why do we need that in the first place ???

    Cant we just remove the static word and declare a new main method.. ???
    This whole thing about static is really confusing me...

    I hope somebody can explain that and I wish that maybe someone can post a simple little program illustrating the purpose of the static in methods and variables.

    Hope u wont find this a dum question.. any help would be appreciated.
    Thanks.

  2. #2
    Leprechaun is offline Member
    Join Date
    Dec 2007
    Posts
    30
    Rep Power
    0

    Default

    Here's my understanding about the difference between static and nonstatic methods...
    Static methods will always be the same for every object they are in. So lets say you have a Circle class, you could have a static method called printAreaFormula().. which would always print: "3.14 * r * r" (or similar).

    Nonstatic means that the object can be different. For the circle class you could have a circle object with a radius of 3 and one with a radius of 4. So if you have a printRadius() function the outcome would depend on the object.

    The main method is static because it always does the same thing (which can vary with paramteres and user input)...

  3. #3
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Leprechaun View Post
    Here's my understanding about the difference between static and nonstatic methods...
    Static methods will always be the same for every object they are in. So lets say you have a Circle class, you could have a static method called printAreaFormula().. which would always print: "3.14 * r * r" (or similar).

    Nonstatic means that the object can be different. For the circle class you could have a circle object with a radius of 3 and one with a radius of 4. So if you have a printRadius() function the outcome would depend on the object.

    The main method is static because it always does the same thing (which can vary with paramteres and user input)...
    I see your point here... but why are we declaring it as a static method ???
    In your example we still can passs values to the parameters even if we dont declare the method (static). I read more than a Java book and they all mention that "Static means that you dont have to create an instance or an object of that static method in order to call it or use it"... and I still dont see what does it mean.. ??
    And this leads me to another question... is it possible to change static methods or variables ???
    Thanks alot for the example.

  4. #4
    Cnu
    Cnu is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    The Word static is used where we want to execute a method without instatiating an object.
    we can write a class without main(). By keeping static block inside class as:

    class ProgrmaWithoutMain
    {
    static
    {
    System.out.println("Program with out main()");
    System.exit(0);
    }
    }

  5. #5
    gibsonsydsa is offline Member
    Join Date
    Feb 2008
    Location
    South Africa
    Posts
    9
    Rep Power
    0

    Default

    A method is declared as static in the following cenario's:

    1. The main method
    2. No instantiation is required.
    3. No Method over-riding will occur.
    4. All instances of a class will use the same method...thus its not necessary
    to allocate unnecessary memory to duplicate the same object(method,
    class or variable) has to do with reduction in overall redundancy and
    improvement of memory management.

    Then to answer your question regarding changing static variables and methods SCS17..

    As I understand it values can be sent as arguments to static methods thus the parameters(the variables that receive values sent to a method) will change but not the method itself.If that makes any sense to you..

    If you are looking to create totally unchangable classes, methods and variables use the final key word instead.

    If possible could a more seasoned developer please shed some light on this post as im sure that I might have missed something.

  6. #6
    praveena is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    This is how i understood why main method is static..
    jvm starts execution of the code from main method of the class ...
    To execute the same main method no matter how mant times the class is being executed we use static..
    final does not this property.. it only says the method cannot be inherited..

  7. #7
    gibsonsydsa is offline Member
    Join Date
    Feb 2008
    Location
    South Africa
    Posts
    9
    Rep Power
    0

    Default

    Praveena,

    You are correct such a method or class cannot be inherited , and it is impossible to add final to the main method and unnecessary as final is used to set an object once and never be able to change again after that eg: if I declare a final variable to contain the fixed interest rate for a banking transaction and do not want this value to change after it has been set then id use the Final keyword.


    Gib

  8. #8
    Leprechaun is offline Member
    Join Date
    Dec 2007
    Posts
    30
    Rep Power
    0

    Default

    The specific part about it not being instantiated would probably be more clear with an example. Look at the Math class... You can use methods such as Math.pow(4,2), pow is a static method (I've never checked this but I think it makes sense). Just like Main(stringVariableOfArguments) is calling a Main method of a class with an argument.

  9. #9
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    so if we take a look at the Scanner method.. each time we want to use it.. we have to declare a Scanner object.. say

    Scanner input = new Scanner(System.in)...
    so I suppose Scanner here is non-Static.. since we decalred an object called input... am i thinking right here ??? Hope so!

    it makes a little more sense now.. i see your point guys.. but I still cant understand why the main method has to be Static.
    Thanks alot for the help!.

  10. #10
    gibsonsydsa is offline Member
    Join Date
    Feb 2008
    Location
    South Africa
    Posts
    9
    Rep Power
    0

    Default

    Yes you are right, Scanner would be non-static in that case.

    The reason that Main is static is that all instances of a class will use the same point of entry...look at it from a performance point of view, why would I want to create a duplicate of a method that will never change and be used only once in the lifetime of an instanciated class..

    Does it make more sense?

Similar Threads

  1. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 01:20 AM
  2. Static methods - not working
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 02:31 PM
  3. Why methods in an interface cannot be static?
    By cbalu in forum Advanced Java
    Replies: 2
    Last Post: 12-12-2007, 08:57 PM
  4. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM
  5. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 10:52 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
  •