Kailani Yung
Kailani Yung

Reputation: 45

Java syntax confusion in OnClickListener

I am a newbie in Android Development, how can I understand this snippets?

private OnClickListener listener = new OnClickListener() 
{   
    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        System.exit(0);
        finish();
    }
};

OnClickListener is a static interface, we can't use "new" to initialize an object. Who can help me, thank you in advance.

Upvotes: 1

Views: 102

Answers (1)

ballpointcarrot
ballpointcarrot

Reputation: 113

In Java, you can create an Anonymous Class if you implement all necessary requirements of the interface being presented. This is acting as a new class definition implementing OnClickListener, and overriding its one necessary method, onClick(View).

Upvotes: 1

Related Questions