File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Static final instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static final instance variables" Watch "Static final instance variables" New topic
Author

Static final instance variables

Anders Hermansen
Greenhorn

Joined: Nov 15, 2007
Posts: 6
Hi

If a user create ten instances of an object with a static final long, and a static final String and a private long, how will the different instances fill up in memory on stack, heap and String pool.

Example:

class A {
static final long firstlong = 99;
static final String s = "ab";
private long anotherlong = 12;

/* more code */

}
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by Anders Hermansen:
Hi

.....how will the different instances fill up in memory on stack, heap and String pool...



Its system/JVM dependent.
If you are interested you can get details here
Anders Hermansen
Greenhorn

Joined: Nov 15, 2007
Posts: 6
Thanks for the answer.

My concern is if a make 10 or 1000 instances af class A, how many references and objects will there be of the static variables ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
How many copies of the static variables?

One of each.
Anders Hermansen
Greenhorn

Joined: Nov 15, 2007
Posts: 6
But every time a make a new instance of class A will there be a reference to String s? An d if yes - will the reference exist on the stack or heap?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Yes, each new instance will have a reference to all your static members. Not certain, but all Objects live on the heap, so I think the references will be from the heap.

There are probably other people better able to answer that question.
Srinivasan Mahadevan
Greenhorn

Joined: Jun 21, 2002
Posts: 5
Only for instance variables, memory is allocated for each instance. For static variables, memory is allocated only once. There is no necessity for each instance to have a reference to static variables memory. Static variables can be accessed just by using the class name. Even though you can refer to a static variable using an object's instance, it is really not necessary and should not be preferred. Even if you go ahead and refer a static variable using an object's instance, the compiler can always know the class object associated with the object's instance and then access the static variable (without really storing any reference to static variable in each object instance).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
By the way, you called this thread "static final instance variables." A static variable isn't "instance."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Campbell]: Yes, each new instance will have a reference to all your static members.

But the reference isn't held by the instances, so each new instance doen't have any effect at all on the number of static references. I beleive that's what Anders was asking about.
[ January 07, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Yes, I think you are right, Jim Yingst. The references would go via the Class object, so there is only one actual reference for the objects to find. Is that right?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Jim Yingst:

But the reference isn't held by the instances, so each new instance doen't have any effect at all on the number of static references. I beleive that's what Anders was asking about.



Very true Jesper.
[ January 08, 2008: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Anders Hermansen:
...But every time a make a new instance of class A will there be a reference to String s?


Upto my knowledge the answer is No, provided if your question was supposed to be of "will there be a reference to String s in each instance?"

As Jesper and Srinivas quoted, the instances of a class are not required to bother about having their own references to the static members.

Of course, that's the whole point in having a static member. Right?

If at all, you think each instance will have a reference to reach the static members of a class, it will be a mere waste. Moreover, it will be one more level of indirection when you are sure about the class via the instance and through which you can better be reaching the static members.

I believe this makes some sense!
[ January 08, 2008: Message edited by: Raghavan Muthu ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jim Yingst]: But the reference isn't held by the instances, so each new instance doen't have any effect at all on the number of static references. I beleive that's what Anders was asking about.

[Raghavan Muthu]: Very true Jesper.


Thank you Rajiv.

[Campbell Ritchie]: The references would go via the Class object, so there is only one actual reference for the objects to find. Is that right?

Yes.
[ January 08, 2008: Message edited by: Jim Yingst ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Anders Hermansen:
..An d if yes - will the reference exist on the stack or heap?


The objects do certainly live in the Heap memory.

And the references are like another variables, they are supposed to live in Stack Memory.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Jim Yingst:
..

Thank you Rajiv.


Rajiv??
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Raghavan Muthu]: The objects do certainly live in the Heap memory.

Not always. Since JDK 6 the JVM will sometimes allocate objects on the stack instead, if it can determine that the object is only accessible from within the current method. This is called escape analysis.

[Raghavan Muthu]: And the references are like another variables, they are supposed to live in Stack Memory.

I think Anders was asking about references like the variable s, which is an instance variable. (Even though it's also a constant.) Instance variables generally exist on the heap, as part of the instance. Unless the instance is on the stack, as just discussed.

Local variables are always on the stack. Instance variables are located with the instance, generally on the heap. Class (static) variables are located with the Class object, which should be always on the heap.

In general though, you don't really need to know or care where a Java object exists in memory. If it helps your understanding to know this stuff, great. But if it gets confusing, it really doesn't matter anyway, unless you're building your own JVM.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jim]: Thank you Rajiv.

[Raghavan]: Rajiv??


Take another look at your previous replies, particularly the part I quoted.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Jim Yingst:

Not always. Since JDK 6 the JVM will sometimes allocate objects on the stack instead, if it can determine that the object is only accessible from within the current method. This is called escape analysis.


Thank you Jim. I have not been aware of this! Nice to read about "escape analysis".


I think Anders was asking about references like the variable s, which is an instance variable. (Even though it's also a constant.)


In his case, the "s" was a class variable.


Local variables are always on the stack. Instance variables are located with the instance, generally on the heap. Class (static) variables are located with the Class object, which should be always on the heap.


Yes, very true. What i did mean to say was the *local* variables even though they are reference variables.

But to his original question, the reference variable s to a String object being a static one, the answer should have been heap as you said.

Thank you.


...But if it gets confusing, it really doesn't matter anyway, unless you're building your own JVM.


Perfect!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Jim Yingst:


Take another look at your previous replies, particularly the part I quoted.


still i do NOT get you Jim. where you got the name "Rajiv" here?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Where do you see the name "Jesper" here?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Ouch.. I am sorry

Got confused with Jim and Jesper! I have become an ardant fan of many bartenders like Corey McGlone, Jim Yingst (You ), Jesper Young, Marc Weber, Ben Souther, Bear Bibeault, Ulf Dittmer, Peter Chase etc etc., [Needless to say about Kathy, Bert ]. And the list goes on gradually!

Recently i had been seeing Jesper's replies at many places and thereby the confusion as i think the term 'Jesper' had been in TOS

Kindly bear with me Jim! [See, this time i am 100% right! ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

To be frank and honest, all the names in full are typed from my memory cache without any reference, copy-paste etc!

Anders Hermansen, Sorry for the situation which happen to hijack/divert the thread!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static final instance variables