• 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

Scope of non-static variables in static objects

 
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to understand a piece of code i have encountered.

Following is a similar piece of code:



What is the scope of the 'map' variable if i create a static instance of the AbstractParent via the child ?
Map is private to GrandParent (i.e. at object level) and the object of AbstractParent is static (i.e. at class level). Could there
be a possibility that 'map' variable could be eligible for garbage collection even when AbstractParent(Child) instance exists ?

Secondly, what could be the reason behind this kind of design ?

I tested the aforementioned code to have a non-static reference to the static instance of AbstractParent as in the following example:



and found that the map contains all the 20 elements. Can anyone explain the reason behind this ? I guess i am missing something very basic over here.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects aren't ever static. References may be static.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Objects aren't ever static. References may be static.



Can you please elaborate ? How does it relate to the scope of the 'map' variable. As it can be seen that 'Map' is private to GrandParent
and we are creating an object of Child whose reference is static, hence, map shouldn't be in the scope of Child or should it be ?
Plus, 'Map' shouldn't be visible to Child as well as AbstractParent.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To simplify my question, i would like to know the scope of the static reference variable over here.
Theoretically, a static variable's scope is available as long as the class is loaded i.e. as long as the class remains in JVM.

When does a class get unloaded from JVM ? Does it ever happen ?
In my example, how long will the object referenced by the static variable be there in JVM ? As long as the JVM exists ?
IS that the reason why the 'map' variable will be present with the object since, its a part of the object ?


Thanks,
Adithya
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're confusing terms.

There's no such thing as a "static object", as Darryl already noticed. Member variables, such as the member variable parent1 in class AbstractParent can be static, but objects cannot be static. Note that a variable is not the same thing as an object - a variable is a reference to an object.

Also, the scope of a variable does not directly have a lot to do with when classes are loaded or unloaded or when an object becomes eligible for garbage collection. The scope of a variable defines where in the source code you can and cannot "see" the variable. If you attempt to use a variable at a place in the source code where it's not in scope, the compiler will give you a "cannot find symbol" error. Scope is a compile-time concept, while things like when a class is (un)loaded or an object is eligible for garbage collection are runtime concepts.

When a member variable is static, it means that there is only one copy of that member variable, which is shared by all instances of the class. When a member variable is not static, then there's a separate copy of the variable for each instance of the class.

The variable map in class GrandParent is not static. That means that each instance of GrandParent (including instances of subclasses of GrandParent) will have its own instance of the map variable.

The variable parent1 in class AbstractParent is static. That means that there's only one instance of this variable, which is shared between all instances of AbstractParent (including subclasses). Note that the object that parent1 refers to has its own instance of the variable map.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic