This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

java general

 
Ranch Hand
Posts: 32
Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.).
 
author
Posts: 23924
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Goodbye moon men. Hello tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic