• 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

Method Local Inner Class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I'm new to Java and to this group. I'm studying for the SCJP exam. I have a question regarding Inner Classes.

In the code below I have a method doStuff() that returns a reference to MyInnner class. How can I use this reference to invoke the seeOuter() method in the instance of MyInner class?



( tags added )
[ December 07, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code solves the problem:

try{
obj.getClass().getMethod("seeOuter", null).invoke(obj, null);
}
catch(NoSuchMethodException nsmx){
System.out.println("NoSuchMethodException: "+nsmx.getLocalizedMessage());
}
catch(IllegalAccessException iax){
System.out.println("IllegalAccessException: "+iax.getLocalizedMessage());
}
catch(InvocationTargetException itx){
System.out.println("InvocationTargetException: "+itx.getLocalizedMessage());
}
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put this part of code in ur main section.

========================================================================

MyOuter5$1$MyInner inner;
inner = (MyOuter5$1$MyInner) obj;
inner.seeOuter();

========================================================================
 
Alf Norvik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for answering my request.

I have tested both, but only the first (from Dumitru) compiled and executed OK. The second didn't compile. I got this message from the compiler: "The type MyInner is not visible".


Alf
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anand Koppal:
Put this part of code in ur main section.

========================================================================

MyOuter5$1$MyInner inner;
inner = (MyOuter5$1$MyInner) obj;
inner.seeOuter();

========================================================================



No way! You must never use any knowlege of the class files generated for inner classes by the compiler!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that the scope of the class MyInner is the body of method doStuff and there is no non-convoluted method to reference it outside that method. You had to use Object as the return type of method doStuff because you could not use MyInner. The alarm bells should go off giving a strong hint that the design smells a bit off.

Beyond the scope of SCJP anyway
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this will work just fine (mind I created an interface just for the show, any interface will do).



You can just reference the returned instance as an implementation of the interface, the actual class type isn't relevant to the calling function at all.

If this example is convoluted it's because I deliberately designed it to be completely self-contained.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar idea to Jeroen's on the way to work this morning, using an interface that is:



Why on earth you want to such a thing is beyond me at the moment
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then we can junk class MyInner and use only interface MyInnerI:
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
personally I hate anonymous inner classes and for that reason would use a method local inner class more naturally (and even those I rarely if ever use).

But it's on the exam so we think about it which keeps the brain working on a slow day
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alf,
I hope this answers your question. I know it is way out of SCJP scope, but I digged more in to the classes hierarchy.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic