0
class A            // Normal Class
{  
  static int i;    // Static int variable  
  int j;           // Normal int variable  
}  

Suppose I create two objects namely A1, A2.
Do both objects have separate memory allocated for the variable i or the common, Permgen space

What would be the size of A1, A2? If suppose the size of int is assumed as 2 bytes.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
Harsha
  • 349
  • 1
  • 8
  • 20
  • 3
    It's static. It has no relation to instances of the class. – Marko Topolnik Oct 07 '14 at 08:22
  • What would be the size of each instance?? if the size of int is assumed as 2 bytes – Harsha Oct 07 '14 at 08:25
  • 2
    Why would you assume size of int as 2 bytes? An `int` property occupies at least 4 bytes, and possibly 8 due to memory alignment concerns. Java objects also have significant memory overhead and a `new Object()` costs 24 bytes on 64-bit HotSpot. – Marko Topolnik Oct 07 '14 at 08:32

5 Answers5

6

Same memory, static member variables are shared between instances because static is class level and they are both of the same class.

As for the size of the objects, they would the the size of the object minus the static member variables.

Also

  • static has a lowercase 's', not uppercase. Keywords are case sensitive.
  • int is 4 bytes, not 2
Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • I want to know wether the size of var i, is added to the instances size. would the objects size be (size(i)+ size(j)) or (size(j)) only. bcoz the var i is stored seperately – Harsha Oct 07 '14 at 09:44
  • `i` is not added to the object size, it's basically treated as a separate global variable accessed only by that class. – Ross Drew Oct 07 '14 at 09:48
3

Does both the objects have seperate memory allocated for the variable i?.

Static members are at class (and not instance) level. So, there will be only one int i. j is at instance level, so each instance of A will have a j in it.

So, your question is kind of invalid. Why? because static has no relation with instance of class (Like Marko Toplonik says)

instanceOfA.i will actually be resolved to A.i

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

What would be the size of each instance

The size of the object depends on instance members only.

How to prove it?

Calculate the size of object of type A and check what is prints and then do the same without static variable in class A?

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(new A());

System.out.println(baos.size());
Braj
  • 46,415
  • 5
  • 60
  • 76
0

Static instance are declared in the inside of the class template. Compiler is load the class to the memory only one time, and using this template compiler is creating byte code. JVM is creating object by using this class template and do not load more and more same class template to create more and more objects. Therefore static instance are declared only one time. Every object of the same class are access the same static instance.

Good Luck !!!

0

Static variables are loaded at time when the class is loaded, so they are allocated a constant memory space irrespective of number of objects created.

So, the memory for the variable i remains same for all the objects created.

You can even access the static variables and functions without creating the objects, just by using the class name.

class A
{
    static int i;
}   

A.i=10;
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133