• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous classes

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class main{
class class1{
int i;
int ret(){
System.out.println(this.i);
return i;
}
}
class class2{
int k = 100;
int ret(){
System.out.println(this.getClass());
new class1(){ public int ret(){
System.out.println("Anonymous class");
return k;}
};
return k+1;
}
}
public static void main(String arv[]){
main m1 = new main();
System.out.println(m1.new class2().ret());
}
}

Q1 - Why this class does not print "Anonymous class" and 100.It just prints main$class2 followed by 101.?
Q2 - How it will print "Anonymous class" and 100?
Q3 - Can we define new methods in 'Anonymous' classes.If not. Why?
Q4 - Can we explicitly call methods on anonymous classes.If yes, what is the syntax in context of above main() method?
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Q1 - Why this class does not print "Anonymous class" and 100.It just prints main$class2 followed by 101.?
U r creating instance of Anonymous class , but no where assigning to a reference.
------
Q2 - How it will print "Anonymous class" and 100?
We can change ret() method in class2 as below.
int ret()
{
System.out.println(this.getClass());
class1 obj=new class1(){ public int ret()
{
System.out.println("Anonymous class");
return k;
}
};
return obj.ret();
}
--------------------------------
Q3Can we define new methods in 'Anonymous' classes.If not. Why?
We can define new methods.But we will not be able to call it from outside as it is referring through its Parent classes (or interfaces).
Fallowing code is possible , but we should not fallow it.
Here , i have just defined one explicit method called Try() in Anonymous class and i called it from outside.It seems bo be very cryptic
(new class2(){
public int ret()
{
System.out.println("Anonymous class");
Try();
return k;
}
public int Try()
{
System.out.println("Inside Try
method");
return 420;
}
}).Try();

Hope i cleared ur questions.
----------
Prashant
 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx Prashant!
Still i have a question. You have tried to call the method Try() in the enclosing class 'class2', but i want it to be printed from the main method as i have created there an instance of 'class2'. Can u follow plz.
 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveed!!
I think it is not possible to call Try() from main method.Coz,to call its method , i need to get its reference which can be of its Parent type(ie, class2).So we can only call overridden methods of Anonymous class not its specific methods from outside world.
Pls let me know if u find any resource on these aspects.
--------------
Prashant
 
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic