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
{
static int x=0;
MyStat()
{
x++;
}
public static void main(String arg[])
{
MyStat m1=new MyStat();
MyStat m2=new MyStat();
MyStat m3=new MyStat();

m1.x=80;
m2.x=90;
m3.x=56;
int mx=m1.x;
System.out.println("MyStatic variable: "+mx);
}
}

why is 56 being printed instead of 80.?
I understand Static variables belong to a class,rather than being asociated with an individual
instance of a class.
By changing the value of the static variable through an instance,does it mean that
we can change the value of a static variable?
Static methods are not overridden(to be nonstatic).How about Static variables? Whats the point in declaring
variables as static when they are flexible to change through instances?
I think i am confused between features of "final variables" and "static variables"
A final keyword provides restriction to use methods or variables differently.What exactly
does static keyword do?

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.
Other Gurus, please correct me if I'm wrong.

Prabhu.

satya5
ranch hand
posted April 10, 2000 12:57 PM             
Aparanji:

As it is said, Final variables CANNOT be modified.
static variables can.

There can be a lot of uses of static variables.
Say I have a class and want to keep track of who
instantiated it last, I can declare a static variable
called "lastAccess" as an int or String and store the
calling method/class (assuming the caller gives me) name
or some form of ID.

The example what you mentioned above is exactly the
same as what is described in RH (page 84-85).

I thought the explanation was good.
Hope this helps.

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
maha anna

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!

|