Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java general

 
Gangireddy Danam
Ranch Hand
Posts: 32
Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class P{
public void pop()
{
System.out.println("P");
}
}
class Food{
P ob=new P(){
public void method1()
{
System.out.println("hai..");
}
public void pop()
{
System.out.pritnln("sir");
}
};
public void popit()
{
ob.pop();//it will execute
ob.method1();//it will not execute..
}
}
...if i want to call method1()..how could may i call..?
can anonymous classes will replace constructors..?

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you edit your post and put code tags around the java code? It is very difficult to read otherwise.

like so:



also post your real code. This will not compile as is (typos, etc.).
 
Henry Wong
author
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gangireddy Danam wrote:...if i want to call method1()..how could may i call..?
can anonymous classes will replace constructors..?


Basically, using a Pop reference, you can't, as the Method1() method is not in scope for the Pop class. The likely / common answer is that you need to cast the variable, but since it was instantiated from an anonymous class, the class definition is out of scope -- and hence, you can't cast it either.... So, your only option is to use the reflection libraries.

Henry
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic