Author | Topic: A basic qn regarding static keyword. |
Aparanji Raju greenhorn |
posted April 10, 2000 12:34 PM
Hi all!I've been reading R&H for SCJP exam.I got the following doubts when reading abt static variables and methods. class MyStat m1.x=80; why is 56 being printed instead of 80.?
|
Prabhu greenhorn |
posted April 10, 2000 12:42 PM
"Static" Keyword basically tells that there'll be only one copy of the variable, irrespective to the number of instances created. So, when u change the value of a static variable, the last changed will be stored in the variable. On the other hand, "Final" means that the value cannot be changed during the course of the program. Hope this helps. Prabhu.
|
satya5 ranch hand |
posted April 10, 2000 12:57 PM
Aparanji: As it is said, Final variables CANNOT be modified. There can be a lot of uses of static variables. The example what you mentioned above is exactly the I thought the explanation was good. Btw Prabhu, I am not a Guru in this :-) Regds. - satya [This message has been edited by satya5 (edited April 10, 2000).]
|
maha anna bartender |
posted April 10, 2000 01:11 PM
Raju, What Prabhu said is correct. A 'static' var is shared by all instances (objects) of the class. There is only one copy of the static var in memory and whenever you change its value, it is seen by ALL objects. Generally you put all states (vars) which you think are COMMON to all objects created from the class as static. And put all others as instance (member) var which you think should belong to a particular individual object and SHOULD NOT be shared by ALL. THis is the key. Simillary there is only one place where the static method code resides in memory which is invoked by ALL objects whenever this staticmethod is called. This leads to the restriction of 'static methods can't use instance vars'. This is logical right?. If a method belongs to COMMON for ALL then, how can it access a particular objects owned copy. It is not fair right? So only in Java, a static method can access all static related var/methods/inner classes stuff. the other point which you said about 'static methods can't be overriden by sub-classes' is completely different concept. The subclass is also a seperate class. It can have it's own version of static stuff. 'static means what? COMMON for ALL objects derived from THAT PARTICULAR class. which means shouldn't a subclass have its own version of COMMON BEHAVIOUR for its object? SHould it use the same inherited static stuff from its base class? No. It is not fair either. But the subclass is somehow related to the base class, it has to respect some behaviour define from its base. When the base class declares some stuff as COMMON for all , the sub-class should follow that, it can't say it wan't this static stuff to be non-static. Because the subclass born from the base class. If the child want's to have some stuff which are alreadu declared common by its parent , then it can't contradict that. Maximum it can say, Ok I follow that but I want to re-define the behaviour atleast. This is a compromise. The subclass can re-define the static method's body/redeclare the static to have different val etc. regds
|
Aparanji Raju greenhorn |
posted April 10, 2000 01:31 PM
Thanks a lot Prabhu,Satya5 and maha anna.Your explainations have cleared up the confusion in my head. ) Edited by Maha. put a semicolon and a ) [This message has been edited by maha anna (edited April 10, 2000).]
|
Aparanji Raju greenhorn |
posted April 10, 2000 01:37 PM
Uh oh!I dont know how that sad icon came about.I had typed a smiling face and was quite shocked to see a dullone. Anyway,Thanks all again!
|
| | |