mre
mre

Reputation: 44240

Is this a variation of an anonymous inner class?

Here is an example

JPanel panel = new JPanel(){
    @Override
    protected void paintComponent(Graphics g){
        // do stuff
    }

    @Override
    public Dimension getPreferredSize(){
        // do stuff
    }
};

Would this just be a variation of an anonymous inner class, or is it something else entirely?

Upvotes: 3

Views: 409

Answers (3)

Jim Gish
Jim Gish

Reputation: 71

You may be confused about the anonymity of the class because at first blush it looks like you're defining panel to be an instance of JPanel. However, that's not what you are doing. Instead you are defining a sub-class of JPanel, which is a new class and creating panel to be an instance of this new sub-class. What is the name of this new class? Well, it doesn't have one and hence that's what makes it anonymous!

Upvotes: 5

Kaypro II
Kaypro II

Reputation: 3340

That is an anonymous inner class.

Upvotes: 1

Nathan Romano
Nathan Romano

Reputation: 7096

Yes that is an anonymous inner class

Upvotes: 7

Related Questions