Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Classes

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is given in k&B
It didnt have a main() method - so I added one inorder to make the program run.
I understood the concepts ,but I dont know how to make the overiding method run.That is the pop() in Food Class.

How am i going to creat an instance when the class doesn't have any name ie. anonymous.

I also wanted to check out if the code runs with out the semi-colon at the end of the class,which mentioned in the exam watch.
So couldn't check that out either.



 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

you can make p reference static and can call p.pop() in main method.



otherwise you can create instance of Food in main method and can call as below

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramesh maredu:
Hi
static Popcorn p=new Popcorn() {
public void pop()
{
System.out.println("Anonymous Popping Hahaha!!!");
}
};


public static void main(String[] args)
{
p.pop();
}

[/CODE]

otherwise you can create instance of Food in main method and can call as below

[code] Food food=new Food();
food.p.pop();
[/code]


Hey Ramesh,
Thanks for the reply.
Sorry for the late response.

If you don't mind could you please help me here with something..

From what I have understood so far,variable p is the instance of the anonymous class which is a subclass of Popcorn.
How can you make it static when it is an object ( or reference variable).

In the second case,
How are you calling it.
Are you calling it like - "ClassName.InstanceVariable.Function()"
Is not making any sense..
(Even though the code is working great)

It would be great if you can help me out here.

Thanks.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you make it static when it is an object ( or reference variable).

Nothing wrong in making object reference as static,for example in java.lang.System class we have a static variable called "out" which is a reference to java.io.PrintStream.and we can call println method on the out reference,all put together,we can call println method as below

System.out.println();

I guess it can answer your second question also.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramesh maredu:

Nothing wrong in making object reference as static


Is this a special case for anonymous class or something.

Because if I create a regular object of a class I get a compilation error
"illegal start of expression"



What exactly is the difference between the two.
[ May 22, 2008: Message edited by: Nabila Mohammad ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case object reference h is a local variable local variables can not be static,if object reference is instance variable we can make it satic it is applicable for all
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot!
That helps!
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use the keywords public private and static inside a method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic