prasad
prasad

Reputation: 61

Static variables in java

I have scenario like this

public class Test{

   static int a;
   int b;

   public static void main(String[] args){

      Test t1  = new Test();
      Test t2 = new Test();

   }
}

What will be the variables in object t1 and object t2?

As per my understanding since variable a is a static variable it will be both in object 1 and object 2.

And b will be created separate copy for both the objects.

But when I assign a value to variable b ie(int b=1) and call it like System.out.println(t1.b), System.out.println(t2.b)

Instead of getting an error I am getting 1 as output from both the objects.

Why is that?

Upvotes: 1

Views: 3221

Answers (6)

raviteja
raviteja

Reputation: 1

static variables are class variables. static variable are stored in static memory.

static variables are created to access inside main() method. The static variables can be accessed by their class name, if they are present in outside class.

class sv {
  static String name = "abc";
}

public class staticvariable {
  static int t;
  public static void main(String[] args) {
    System.out.println("static variable is - "+t);
    System.out.println(sv.name);
    t=100;
    System.out.println(t);
  }
}

Upvotes: 0

roottraveller
roottraveller

Reputation: 8232

Static variables are class members that are shared among all objects. There is only one copy of them in main memory. Static variables are created at run time on Heap area.

In case of yours...

int b = 1; 

it's an assignment at class level, making 1 the default value for variable b(In normal case default value is 0). hence when you print it, its gives you the ans as 1 not error.

int b = 1; //initialized b to 1 for all obj of class Test
System.out.println(t1.b); // Prints 1
System.out.println(t2.b); // Prints 1

Upvotes: 3

ABHISHEK RANA
ABHISHEK RANA

Reputation: 327

Here a is static variable and b is instance or non-static variable.

For static variable, only one copy is created for all objects, where as for instance variable, one copy is created for each object. Here when b=1, for every object of that Test class, one copy is created and it can access it through its object name. So output will be 1.

Upvotes: 1

Tri-Sky
Tri-Sky

Reputation: 1

Of course that is exactly what it should output. If you assign say,

int b=1; 

that assignment is at class level, making 1 the default value for variable b. However, if you assign like this:

t1.b=1;

that will assign 1 to only the copy of variable b in object t1.

Try it.

Upvotes: 0

jfcorugedo
jfcorugedo

Reputation: 10059

No, a static variable is not an instance variable but a class variable.

That's, every instance of this class shares the same reference of those variables.

Take a look at Understanding Class Members

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503429

As per my understanding since variable a is static variable it will be both in object 1 and object 2.

No. It's not in either object. It exists without reference to any particular instance at all. Although Java allows you to refer to it "via" a reference, e.g. int x = t1.a; you shouldn't do so. You should instead refer to it via the class name (test.a in your case - although you should also start following Java naming conventions) to make it clear that it's static.

And b will be created separate copy for both the objects.

Yes.

But when I assign a value to variable b ie(int b=1) and call it like System.out.println(t1.b), System.out.println(t2.b) Instead of getting an error I am getting 1 as output from both the objects.

Well that's because you've basically given it an initial value to assign for each new object. That's entirely reasonable. There are still two independent variables:

t1.b = 2;
t2.b = 3;
System.out.println(t1.b); // Prints 2
System.out.println(t2.b); // Prints 3

Upvotes: 9

Related Questions