Reputation: 1304
In my java program,I need to store the recent values in a variable and my code is as shown below
public class Exmp2
{
int noOfInstances;
public Exmp2()
{
noOfInstances++;
}
public static void main(String[] args){
Exmp2 e1=new Exmp2();
System.out.println("No. of instances for sv1 : " + e1.noOfInstances);
Exmp2 e2=new Exmp2();
System.out.println("No. of instances for sv1 : " + e2.noOfInstances);
System.out.println("No. of instances for st2 : " + e2.noOfInstances);
Exmp2 e3=new Exmp2();
System.out.println("No. of instances for sv1 : " + e3.noOfInstances);
System.out.println("No. of instances for sv2 : " + e3.noOfInstances);
System.out.println("No. of instances for sv3 : " + e3.noOfInstances);
}
}
My output should be 1 2 2 3 3 3 but am getting 1 1 1 1 1 1 can you give solution?
Upvotes: 5
Views: 13362
Reputation:
You have to declare you variable as static : static
variable always stores the recent values,its static variable would store in static pool
static int noOfInstances;
Upvotes: 2
Reputation: 997
See the below example on how to use the static variable for instance count
package com.stackoverflow.test;
public class Exmp2 {
static int noOfInstances;
public Exmp2() {
noOfInstances++;
}
public static void main(String[] args) {
System.out.println("No. of instances at this point "
+ Exmp2.noOfInstances);
Exmp2 e1 = new Exmp2();
System.out.println("No. of instances at this point "
+ Exmp2.noOfInstances);
Exmp2 e2 = new Exmp2();
System.out.println("No. of instances at this point "
+ Exmp2.noOfInstances);
Exmp2 e3 = new Exmp2();
System.out.println("No. of instances at this point "
+ Exmp2.noOfInstances);
}
}
Upvotes: 1
Reputation: 3062
You Should declare your variable int noOfInstances;
as static
, in your code the instances are taking default value 0.
Upvotes: 2
Reputation: 10543
Make noOfInstances
static like given below,
static int noOfInstances; // static are shared among all objects created.
and no need to call e1.noOfInstances
instead you can call Exmp2.noOfInstances
Instance (non-static) variable are copied in Objects whereas static variables are not copied in object. static are at the class level. Every object can see it.
Upvotes: 2
Reputation: 45070
Declare your noOfInstances
variable as static
.
static int noOfInstances;
Since its not static
, for every new Exmp2()
a noOfInstances
is created for that instance, with the default value as 0
.
Upvotes: 7
Reputation: 10871
Whenever you do new Exmp2();
then noOfInstances
assigned to 0 as you are instantiating a new Object, Make noOfInstances
as static
so that its scope shifts to class level.
Upvotes: 1
Reputation: 41955
The variable should be declared static
.
Why? Because presently your variable noOfInstances
is not static and for each instance of your class you create noOfInstances variable will also be created and always the value will be 1.
So on declaring it static it is shared between all the instances of this class and will have the correct value.
Static variables are created when the class is loaded and are shared across all the instances.
Upvotes: 2
Reputation: 2155
noOfInstances should be declared static. For example:
static int noOfInstances;
This might be an interesting read. It should have an example with a similar situation as yours:
In short, static literally makes a variable shared among instances of a given class. If you have a variable that is not static every instance will have its own private value for that variable. While a statically declared variable will have the same value in all instances of a class.
Upvotes: 2
Reputation: 19786
you havew to declare noOfInstances
to be static
static int noOfInstances;
otherwise every new instance create by new
will have its own value of noOfInstances
, beginning at 0 again
Upvotes: 3