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
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 [B]new[/B] 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
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
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