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

Can u plz explain the anonymous inner 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
why do we need this anonymous inner classes in programming practice. I am in the development for 3 yrs. but i have not used this so far. its bit not clear to me.. can anyone explain this with a proper example.. if any proper materials regarding this??plz pointout..

class PopCorn {
public void pop(){
System.out.println("pop 111");
}

public static void main(String[] a){
PopCorn p1=new PopCorn();
p1.pop();

Food f=new Food();
// how can i call the inner inner class object of PopCorn of class Food here ???
}
}
class Food{

public PopCorn p=new PopCorn(){
public void pop(){
System.out.println("pop here");
}
};


}

how can i call the inner class object in my mentioned commented place
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anonymous Inner classes are implicitly final and can never be instantiated.

Cheers,
Kits
All smart guys are not dumb and all dumb guys are not smart.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nadarajah chenththuran:
why do we need this anonymous inner classes in programming practice. I am in the development for 3 yrs. but i have not used this so far. its bit not clear to me.. can anyone explain this with a proper example.. if any proper materials regarding this??plz pointout..

class PopCorn {
public void pop(){
System.out.println("pop 111");
}

public static void main(String[] a){
PopCorn p1=new PopCorn();
p1.pop();

Food f=new Food();
// how can i call the inner inner class object of PopCorn of class Food here ???
}
}
class Food{

public PopCorn p=new PopCorn(){
public void pop(){
System.out.println("pop here");
}
};


}

how can i call the inner class object in my mentioned commented place



maybe you can try to insert this if no one has answered your question yet:
f.p.pop();
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could probably program throughout your entire life without using an anonymous class. That doesn't, however, mean that they don't have some nice uses.

Check out this blog entry and see what you think.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kitty Dayal:
Anonymous Inner classes are implicitly final and can never be instantiated.

Cheers,
Kits
All smart guys are not dumb and all dumb guys are not smart.



Anonymous classes are implicitly final but that does not mean that they cannot be instantiated...it only means that they cannot be sub-classed or extended from. Final classes can be instantiated !!
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your anonymous class is actually a sub-class of class Popcorn
So your reference variable p is actually referring NOT to an instance of Popcorn, but to an instance of the anonymous class!!

So if you want to call the method pop() of the anonymous class from within the class that it is defined in (Food) then you would simply call it like so..

p.pop()

Anonymous classes are used to override methods of the superclass..
Thus, only methods ALSO defined in the super class can be invoked. Even if u define new methods in your anonymous class...you will not be able to call them since you will have to use the reference variable (p in this case) which is of type Popcorn(a parent class) (Ref olymorphism)

you cannot create new instances of the anonymous class...They can have only one instance ..the one tht is created where u actually define your anonymous class.
So if you want to use your anonymous class method which is overrides the parent class method then you have to use the reference variable (p in this case)

However if you wanna call the anonymous class method from outside the enclosing class(Food)
thn you would have to use the following syntax..

f.p.pop()
as suggested by Ann.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main use of anonymous inner classes in Java is the Event Handler Classes.
When we build collections of abstract classes it can help too.
There would have no problems to declare every class to be used, but it'a a nice shortcut.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic