This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can't see an inner class' method from another method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can Watch "Can New topic
Author

Can't see an inner class' method from another method

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
Hello,

This is a simple JTable creation using a TableModel. I made an inner class, MyTableModel which extends the AbstractTableModel. I have properly implemented all other required methods. And then I added an additional method called "fireChanges()".

However, when this fireChanges() was invoked from another method, the compiler couldn't see it.

Any ideas why? I'm wondering if this code is correct in the first place:
JTable table = this.makeTable();
TableModel model = table.getModel();


[ August 11, 2008: Message edited by: Denise Saulon ]

SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCMJEA in progress...
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Hi,

I'm not sure how your program works. But in order to access a method of an non-static inner class. You would need a instance of the outer class(enclosing class) and an instance of the inner class.

In this case, the myMethod would look similar to:

Inner innerInstance = new MainClass().new InnerClass();
innerInstance.myInnerMethod();

or

Outer outerInstance = new MainClass();
Inner innerInstance = outerInstance.new InnerClass();
innerInstance.myInnerMethod();
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
I managed to find my way.. This is why:

TableModel model = table.getModel();

The TableModel reference has no fireChanges() method. I changed the line to
MyTableModel model = (MyTableModel) table.getModel();

and now it can see the method.

Thank you for taking the time to help me out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't see an inner class' method from another method