• Post Reply Bookmark Topic Watch Topic
  • New Topic

static members  RSS feed

 
Vinodh Ramadoss
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do static members reside in heap? if so which represents them? Does object of class Class represent them for each class written in Java
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I believe that all static member objects are allocated from the heap.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to the other part of your question - any instance of that class can reference the member by name:

EXAMPLE:
class Foo {
public static int i = 2;

public void myfunc () {
System.out.println ("i: " + i);
... OR ...
System.out.println ("i: " + this.i);
<= YOU'D ACCESS IT FROM INSIDE THE CLASS JUST LIKE
ANY OTHER MEMBER

Let's say we wanted to access this static member from *outside* the class. Then we could reference it by class name (instead of my class instance name).

EXAMPLE:
Foo f1 = new Foo ();
Foo f2 = new Foo ();

System.out.println ("the static member 'i' is: " + Foo.i);

'Hope that helps .. PSM
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!