• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

inner classes and static things  RSS feed

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above mentioned code, inner class cannot override the static function of the other class (try removing the comments )
but it can derive the same function
and we can call it!
then what exactly is the motive behind
stopping innerclasses from having static data and functions!
somebody please expalin........
------------------
KS
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just thinking aloud...
Static entities, unlike instance entities, exists for longer than the duration of the object. Since these entities belong to the Class rather than individual objects and hence get destroyed when the class is unloaded by the JVM.
By allowing static entities inside a non-static context the concept of "lifetime" of the entity becomes hazy and confusing. Remember C++ where static values inside method retained their old value even after the method terminates? This can lead to bad and non-modular programming. I think that's why the designers of Java language decided not to support this feature.

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalpesh,
The purpose of static is to ensure only one copy of a member exists for the entire class.
An inner class is a member of it's outer class and appears in every instance.
You can create a top-level nested class (an inner class that is declared static). It acts the same as any other top-level class except it must be referenced using both it's name and the name of the outer class.
Example

Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thank you for your reply guys
i am giving the exam on the 12th of feb
buy the way
i have one question
if an outer class has a non static innerclass
is there anything, any method by which an outerclass object can know how many innerclasses it has
i mean does an outer class object knows at all whether it has any relation with inner class or not
or it is only the innerclass object which knows that i am attached to this outer class object but that guys does not know anything !!

------------------
KS
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalpesh,
The outer class would know the inner class is one of it's members; and it would know it could only access the inner class members if it creates an instance of the inner class.
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!