• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All


A static variable of a class will be shared by all instances of that class.
There is only one copy means what ??? it is applicable for the static varaiable and static method.What does it means one copy
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This explanation might clear up things a little.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When ever we declare a variable as "static" in a class,then, only one copy of it is shared by all objects of that particular class.

That means,any changes done by one object for static variable,is reflected in other objects also.

Suppose,there is a static variable x in class A.
and two objects,obj1,obj2 are created for a class A.

If obj1 changes the static var "x",that change is reflected in "x" of obj2 also.

see the following code,

class A
{

static int x=10;
static void show()
{

System.out.println("show in class A");
x=x+10;
System.out.println("x after modification in show() is "+x);

}
}

class statVarDemo
{
public static void main(String args[])
{
int i;
A obj1=new A();
A obj2=new A();
System.out.println(" static var x is"+obj1.x);
obj1.show();
System.out.println("now x in obj2 is "+obj2.x);
}

}

Here x is declared as static and its value is modified in show() method.

Show() is called on obj1.So obj1.x after show()method call is 20.

And obj2.x also prints 20 as x is static var(only one copy for all instance var).

If we remove 'static" modifier from x and from the method show(),

the o/p of obj2.x is 10.

because each instance of class A have its own copy of x.

So,though,obj1 changes value of x,its not reflected in x of obj2.


Hope Iam clear.
 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you vatsalya

Here i have some doubt static members are eligible for GC ???
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation vatsalya gave is not wrong, but not really correct either.
You should see the static members and static methods belonging to the class itself and not to all instances.
For example the usage as in

is not wrong but heavily discouraged and should better be written as

to make it very clear 'x' and 'show()' are static and belong to the class.

Regarding gc, the instances of your class A can be garbage collected as usual. The static members do not belong to the instances and are therefore not garbage collected together with the instances.
The static members are only garbage collected when the classloader unloads the class they belong to (which is unpredictable and might even never happen).
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct Bart Zagers.

I might have used your coding style,instead of mine as your code explains correctly.

Though I know that,static variables and methods belong to class rather than objects,I invoked,static methods and variables with instances.

Thank you for pointing this out.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This comment may go out of bounds for the beginner forum; if so stash it away in your head for later reference. Static variables are "shared by all instances of that class" that are loaded by the same ClassLoader. In complex environments you may find multiple ClassLoaders that can load a class more than once, and keep more than one copy of static variables. One day you may find setting a static variable like "debugging=true" doesn't always work, and this will pop up out of your memory.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic