• 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 member query?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one example of static member. As static member value is shared between different objects the value of counter gets incremented as many objects we create.

The output we get for the static example is 1,2

But say in the below example if i make the counter to int counter=0;
Then the output is 1,1
Does this mean that every object has a copy of its variable like int.


/*
2.
Java static member variable example
3.
This Java Example shows how to declare and use static member variable inside
4.
a java class.
5.
*/
6.

7.
public class StaticMemberExample {
8.

9.
public static void main(String[] args) {
10.

11.
ObjectCounter object1 = new ObjectCounter();
12.
System.out.println(object1.getNumberOfObjects());
13.

14.
ObjectCounter object2 = new ObjectCounter();
15.
System.out.println(object2.getNumberOfObjects());
16.

17.
}
18.
}
19.

20.
class ObjectCounter{
21.

22.
/*
23.
* Static members are class level variables and shared by all the objects
24.
* of the class.
25.
*
26.
* To define static member, use static keyword
27.
* e.g. static int i=0;
28.
*
29.
* Please note that static member variables can be accessed inside
30.
* non static methods because they are class level variables.
31.
*
32.
*/
33.
static int counter=0;
34.

35.
public ObjectCounter(){
36.

37.
/*increase the object counter. Since only one varible is shared between
38.
* all objects of this class, it always return number of objects till now.
39.
*/
40.
counter++;
41.
}
42.

43.
//returns number of objects created till now
44.
public int getNumberOfObjects(){
45.
return counter;
46.
}
47.
}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please re-post your code with CODE tags and without the line numbers; as it stands, it is almost impossible to read.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
Sowm Herur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Reposted it:



This is one example of static member. As static member value is shared between different objects the value of counter gets incremented as many objects we create.

The output we get for the static example is 1,2

But say in the below example if i make the counter to int counter=0;
Then the output is 1,1
Does this mean that every object has a copy of its variable like int.





public class StaticMemberExample {

public static void main(String[] args) {

ObjectCounter object1 = new ObjectCounter();
System.out.println(object1.getNumberOfObjects());



ObjectCounter object2 = new ObjectCounter();
System.out.println(object2.getNumberOfObjects());

}

}


class ObjectCounter{

static int counter=0;



public ObjectCounter(){


counter++;
}




public int getNumberOfObjects(){

return counter;

}

}
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still not seeing any code tags.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Sowm



output is still 1 then 2

static member is shared by all objects.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic