• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim.. Got the point..
Thanks a lot guys..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic