Results 1 to 4 of 4

Thread: about static methods calling....

  1. #1
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    about static methods calling....

    I have a static mehtod in my class. Which is the best way to call the static mehtod using Class or object instance of the class.

    I assume that compiler would resolve the call to the static method if through the Class at compile time and through object instance only at runtime.

    Is compiler is intelligent enough to resolve the call through Object instance at compile time ?

    Please explain this one...

    -------------------
    suresh


  2. #2
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: about static methods calling....

    I think you do not have clear unserstanding of Static methods. For statius methods memory is created at compile time anbyway and everytime a new object is created (using new command), the JVM doesn't allocate memory to the static member function again. IN other words, there will only be one Memory location where static object will be present. And all the objects wil just refer it.

    Unlkie the normal method where in a there will be a function associated with every object that is created.


    So, there is no point in calling a static method through object. I can not imagine why one would want to do it and I have never seen anyone do it either. (Neither in C++ nor in Java).

    Hope that clears things.

    Cheers!
    Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  3. #3
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: about static methods calling....

    So Kalayama you told Class is the best method for calling static methods. But i saw people calling the static method through object also.

    Anyway thanks for your reply....

    -------------------
    suresh


  4. #4
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: about static methods calling....

    Yes. It is possible to call static method though objects. But it serves no purpose unless you modify some varibles that are owned by that object. But, if you do that, it will be poor design because you don't need a static function to do object level jobs. A simple function would be enough.

    So, in general static functions are called through Class. And I am certain it is better programming practice. One can easily identify that the method is static even wthout checking the clas declaration.

    Cheers!
    Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact