• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static objects

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i am facing problems in understanding the behaviour
of static object created at class level.
so any body can plz help me out
thanx in advance
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gareth Leachman:
An explanation that I could offer is to think:
A static object can be references by any instance of this class.


I don't know what you mean by this.


But the static object is only created once and we can use it to keep track of state of the class, or the number of instantiations made.


Salman's question pertained to "static objects". You created a static primitive. Clearly the primitive will hold its same value accross different instances of the 'teststatic' class, but 'i' is not an object.





In terms of the original question... I believe you are referring to inner classes with the 'static' modifier, but I'm not sure what you mean with 'static object created at class level'.

Do you want to elaborate.. I don't quite understand.

ps. You could check a possibly related question I just posted:
[ December 17, 2003: Message edited by: Jim Crawford ]
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
does:
A static object can be referenced by any instance of the class
make more sense?
Also, whether the static item is a primitive or an object is irrelevant in terms of behavior:

The above will print t2 because there is but one instance of class member string for all instances of Class StaticTest.
As Gareth mentioned, a static member can be used to share information between instances of a class, or perhaps to define a 'global' reference value, such as Integer.MAX_VALUE (which is defined as public static final).
Also, a static member is considered to 'exist' even without an instance of that class, Integer.MAX_VALUE is an example of this.
I'll attempt to address your other post at your other post.
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ray Stojonic:
Hi Jim,
does:
A static object can be referenced by any instance of the class
make more sense?


Yep. I could see past the first spelling mistake, but that was it.


Also, whether the static item is a primitive or an object is irrelevant in terms of behavior:


I guess this is true. The only difference I know of, as far as the actual object reference goes, is that static object reference initializers may not throw an exception. I don't know if this qualifies as different behaviour though since after initialization its pretty much like a static 'pointer primitive'.
My line of thought was more related to the static inner class issue as in the other post, but that didn't apply here.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic