aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes 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 "Inner classes" Watch "Inner classes" New topic
Author

Inner classes

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
1) If I have a class defined inside another class, how can I refer to a method in the outer class from inner class, if I have overridden that method in the inner class?

2) What is the use of "outer/inner" keywords???

Thank you guys..
Terry Rickson
Greenhorn

Joined: May 05, 2005
Posts: 22
Hi,

To reference the overridden method of the inner class you use this.methodName(); and to call the outer class method from the inner class use OuterClassName.methodName();

Hope this helps
Terry Rickson
Greenhorn

Joined: May 05, 2005
Posts: 22
To reference the outer method you need to create an instance of the outer class in the inner class and call it from there.

e.g.
[code]
class Outer {
public void method() {
System.out.println("Outer method..");
}

class Inner {
Inner() {
this.method(); // calls the inner class method
Outer o = new Outer(); // create instance of outer from inner
o.method(); // calls the Outer method from inner class
public void method() { // Overridden method
System.out.println("Inner method...");
}
}
}

public static void main(String[] args) {
Outer.Inner test = new Outer().new Inner();
}
}
[\code]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

1) If I have a class defined inside another class, how can I refer to a method in the outer class from inner class, if I have overridden that method in the inner class?

2) What is the use of "outer/inner" keywords???


1) http://qa.jtiger.org/GetQAndA.action?qids=61&showAnswers=true How do I access an outer class member/method from an inner class?

2) There are no such keywords.


Tony Morris
Java Q&A (FAQ, Trivia)
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi..

When I used:

outer_class_name.method(); // Gives me compile error, "non-static method method() cannot be referenced from static context."

But I'm only using it in the inner class method, so why saying "static context" ?

But it works as: outer_class_name.this.method();

Can you please tell me what difference "this" makes for it to work??

Thank you.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Any time the compiler sees some_cless_name.some_method_name(), it figures you're trying to call a static method of some_class_name. Because, well, that's the syntax for making a static method call from outside the class. It has been for a long time; this is nothing new. So if you write outer_class_name.method(), the compiler still figures that method() must be a statid method of the outer_class_name class. If it's not, then the compiler reports an error, because, well, it is an error. This new trick of calling an outer class method from within an inner class is something different from caling a static method. Consequently Java's creators have created a different syntax for it, rather than re-using the syntax for static method calls. They want you to use the syntax outer_class_name.this.method() rather than outer_class_name.method() because they don't want the call to look like a static method call. Because it isn't a static method call. Does that make sense?


"I'm not back." - Bill Harding, Twister
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Jim.. Got the point..
Thanks a lot guys..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes