• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inner classes: How to get the "this" pointer of the parent

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's got to be a trick for using the "this" pointer of the parent object from an instance of an inner class, but I can't find it. (BTW it doesn't help in the research department that "this" is also an extremely common word)


I'm coming from the C++ world and it seems that the use of inner classes is radically different between C++ and Java. In C++ inner classes were literally classes defined within another class and therefore could be instantiated and used anywhere. In Java they seem to be tightly tethered to their parent class: instances can only exist within an instance of the parent and inner class objects can freely use the members of the owning parent object as if it actually was its parent. At first it seemed so strange, but I'm really liking it now.


This is all fine and dandy until I need to use "this". It seems that the "this" within the inner class is literally the object of that inner class instead of the parent object. I've tried super.this and super().this to no avail.


Here's what I'm trying to do:

  • My parent class is a JLayeredPane-derived class with various child components laid upon it
  • My inner class is a MouseAdapter-derived class


  • Within some functions of MouseAdapter I want to convert points from the coordinates of the main parent pane to the child components using SwingUtilities.convertPoint(...). That means I'm going to need to provide the component and the parent pane: and that means I need "this"! But how do I do that?


    I've gotten around this by passing "this" to the inner class' constructor and saving it as a member, but that's such a hack since Java integrates the parent and inner objects so tightly. There's got to be a slick and proper way of doing this.
     
    Sheriff
    Posts: 3837
    66
    Netbeans IDE Oracle Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Parent class usually means a class from which current class has been subclassed. What you're describing is usually called outer class in Java. I'm explicitly stating this because this is an area of frequent misunderstanding for beginners, who can happen to read this thread.

    To get the this instance of a outer class, you'd use OuterClass.this (use the actual class name). And I need to say it took me quite an effort to figure that out when I first needed that.

    ... and welcome to the Ranch!

    Edit: by the way, static nested classes in Java correspond to inner classes in C++. This is a good tutorial covering the topic.
     
    Steve Stevenson
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Martin Vajsar wrote:Parent class usually means a class from which current class has been subclassed. What you're describing is usually called outer class in Java. I'm explicitly stating this because this is an area of frequent misunderstanding for beginners, who can happen to read this thread.

    To get the this instance of a outer class, you'd use OuterClass.this (use the actual class name). And I need to say it took me quite an effort to figure that out when I first needed that.

    ... and welcome to the Ranch!

    Edit: by the way, static nested classes in Java correspond to inner classes in C++. This is a good tutorial covering the topic.



    Ah yes, I was being sloppy with my terminology. It's hard to resist referring to the outer class as a "parent" class since gives birth to instances of the inner one.

    Thanks for the answer to use OuterClass.this. I tried it briefly just to see and it seemed to work. Now I just need to add it where I really need it.

    BTW: Was my description of the differences between the concept of an inner class with C++ and Java correct?
     
    Marshal
    Posts: 80653
    476
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Steve Stevenson wrote: . . . radically different between C++ and Java. . . .

    You should develop a default attitude that everything is radically different in C++ and Java™, until proven otherwise
     
    Martin Vashko
    Sheriff
    Posts: 3837
    66
    Netbeans IDE Oracle Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Steve Stevenson wrote:BTW: Was my description of the differences between the concept of an inner class with C++ and Java correct?


    I think so. But my C++ days are long over.

    The inner class in Java simply keeps a hidden reference to the outer class. This very simple concept allows efficient usage of anonymous inner classes, yet another powerful beast of the Java zoo...
     
    reply
      Bookmark Topic Watch Topic
    • New Topic