• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Getting inner class functionalities without using INNER CLASS

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody tell me, how can we achieved the functionality of INNER class without using INNER CLASS?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you mean by "the functionality of INNER class"?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A clearer problem statement would be nice.

I guess what the original poster wants to know is how can one obtain access to the instance of the enclosing class's methods and data, like an inner class does?

An inner class has an implicit member field containing a reference to the instance of the enclosing class. If you don't want to use an inner class, you can create a class with an explicit member field containing the same data.

If your new class is a static nested class, inside the same enclosing class, then the above should allow you fully to simulate the behaviour of an inner class. While this is true, it is also pointless; why not just use an inner class?

If your new class is a separate top-level class, it will not have access to the private methods and data of the original enclosing class. Nothing you can do about that.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, the JVM knows very little about inner classes, and for the most part they are implemented using regular top-level classes. The compiler basically does the work of converting your inner classes into equivalent top-level classes. You don't see them in your source code, but you can learn about them by examining the generated .class files using javap or a similar tool.

[Peter]: f your new class is a separate top-level class, it will not have access to the private methods and data of the original enclosing class. Nothing you can do about that.

Well, what compilers do, if necessary, is they generate extra hidden accessor methods with package access, allowing classes special access to fields or methods that are private but shoudl be accessible because one class is defined inside another (in the Java source). As far as the .class files are concerned, all classes are top-level classes, but some of them have extra methods that allow other classes in the same package to access their private members.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
In fact, the JVM knows very little about inner classes.



Mhh, I always thought that it knows exactly zero about inner classes. What is that "very little" that it does know?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM class file format has an InnerClasses attribute containing assorted info about inner/nested classes. You can access some of this with reflection - Class has isMemberClass() and isLocalClass(). There's also the Synthetic attribute, which is related in that it identifies members that were not part of the original source file. This is used for generated accessor methods for private data.
 
Well THAT's new! Comfort me, reliable tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic