Static vs Non static
Hi,
I was just not clear as to how does the compiler handle Static members.
As far as fields are concerned if they are declaredstatic, they are created on theHEAP and are shared by all the class instances.
However, what i want to know is what happenes to themethods. I mean methods are just piece of code so why should a non-static method be instantiated for all the instances?
So, I presume thatstatic members mean that they are created on theHEAP.
Am i right here?
Thanks
Praveen
[584 byte] By [
alvandipa] at [2007-11-27 4:46:39]

# 1
Objects are all created on heap.There are a lot of difference between static and non-static.For static methods, they know nothing about the instance for static method call does not push "this" onto stack.That's what I remembered, hoho..
# 2
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
# 3
hello nobody answered my query. I asked WHAT happens in the background in case of static methods. I mean methods are just piece of code. So why this difference, wheather the method is static or non-static should not make any difference. What i want to say is there should be some sort of store where the method's code is put and called irrespective of whether the method is static or non-static
# 4
> ... not clear as to how does the compiler handles Static members.
>
> As far as fields are concerned if they are declared
> static, they are created on the HEAP
Not exactly.
Methods live in the class definition which lives in Perm Space.
> and are shared by all the class instances.
Yes.
> However, what i want to know is what happenes to the
> methods. I mean methods are just piece of code
See above.
> so why should a non-static method be instantiated for all the instances?
'tisn't.
Methods are not instantiated.
> So, I presume that static members mean
> that they are created on the HEAP.
>
> Am i right here?
No.
> I asked WHAT happens in the background in case of static methods.
Why does it matter?
> I mean methods are just piece of code.
> So why this difference, wheather the method is
> static or non-static
> should not make any difference.
The magic 'this' keyword can only be used in non-static methods.
> What i want to say is
> there should be some sort of store where the method's
> code is put and called irrespective of whether the
> method is static or non-static
It is called Perm Space.
# 5
So you mean, the only difference between static and non static methods is that the non static methods can access the 'this' reference where as static methods cannot.What about the data members am i right that static data members are created on the heap and non-static on the
# 6
> > I asked WHAT happens in the background in
> case of static methods.
>
> Why does it matter?
>
You did not answer my question.
> So you mean, the only difference between static and
> non static methods is that the non static methods can
> access the 'this' reference where as static methods cannot.
Saying that is the only difference
could be considered over-simplifying.
> What about the data members am i right that static
> data members are created on the heap and non-static
> on the stack?
Only method local variables live on the stack (and some thread stuff).
Objects never live on the stack - class and instance fields
belong to (are part of) an object.
# 7
the keyword static has multiple implications, overloaded, depending on its context
with fields static modifier implies memory allocation and is intended to be accessed via class (no instance necessary), Foo.staticField
with methods, classes, interfaces it does not have the memory implication -- conceptually it only changes the data it is privy to (no instance hence no this) and is intended to be accessed via class (no instance necessary), Foo.staticMethod(), Foo.Bar.staticMethodInStaticInnerClass()