aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Classes" Watch "Anonymous Classes" New topic
Author

Anonymous Classes

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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.





The future belongs to those who believe in the beauty of their dreams.Dream BIG!
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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



SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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

Joined: Mar 15, 2008
Posts: 210

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

Joined: Nov 05, 2007
Posts: 661
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

Joined: Mar 15, 2008
Posts: 210

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

Joined: Nov 05, 2007
Posts: 661
Thanks alot!
That helps!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You can't use the keywords public private and static inside a method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Classes