Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on about static methods calling.... within the Java forums, part of the Software Development category; 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 ...
|
|||||||
|
|||
|
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 |
| Sponsored Links |
|
|||
|
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!" |
|
|||
|
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
__________________
[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!" |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic and Static lookup | JobHelper | Data Warehousing | 3 | 02-08-2008 10:38 PM |
| Static variables in multiuser environment | Lokesh M | C and C++ | 3 | 12-26-2007 12:25 AM |
| Abstract, final and static keywords | Geek_Guest | Java | 2 | 12-25-2007 09:48 AM |
| Public static final variables in interfaces | ani_lavanya | Java | 1 | 05-02-2007 05:38 AM |
| Adobe Systems calling for tests for Quality profiles | manishsinha | Companies | 2 | 02-08-2007 01:02 AM |