Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner Class  RSS feed

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class test1 {
public void method1() {}
}

class test2 {
test1 p = new test1() {
public void method1() {}
public void method2() {} //where method2 goes ??
};
}

class access {
void access(){
test1 t = new test1();
t.method1(); //perfect i can access to method1
//but HOW can i access to anonymous method2 ??
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't be done (except by using reflection.) The only methods you can access normally are those accessible through the type of the reference variable.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so the method2() is lost in the heap with no reference ?

what is reflection ?

thanks !
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by podonga poron:
ok, so the method2() is lost in the heap with no reference ?


No, a method is not a separate object, so it's not that; it's just that there's no Java syntax to refer to that method.

"Reflection" is a kind of magic way to create objects and call methods based only on Strings containing their names; it's implemented in the java.lang.reflect package. It's not used much in normal programming, but it's used a lot by IDEs, Web containers, JavaBean containers, etc.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!