3 Replies - 737 Views - Last Post: 25 February 2008 - 07:25 PM Rate Topic: -----

#1 c0mrade  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Can someone explain this code? (its short i promise)

Post icon  Posted 24 February 2008 - 10:01 PM

I like to think i'm pretty good at java, but I haven't seen this kind of code before.

Its from the Google Web Toolkit "hello world" program.

public void onModuleLoad() {
	final Button button = new Button("Click me");
	final Label label = new Label();

	button.addClickListener(new ClickListener() {
	  public void onclick(Widget sender) {
		if (label.getText().equals(""))
		  label.setText("Hello World!");
		else
		  label.setText("");
	  }
	});



The part that throws me off is the "{...}" after "new ClickListener()". Is this defining a new method inside the ClickListener() class?

Also, there are statements inside a statement... How does this work? Can I put any statements inside a statement? :blink:

Thanks for the help.

This post has been edited by c0mrade: 24 February 2008 - 10:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can someone explain this code? (its short i promise)

#2 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Can someone explain this code? (its short i promise)

Posted 24 February 2008 - 10:29 PM

It's defining a new inline class.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: Can someone explain this code? (its short i promise)

Posted 24 February 2008 - 10:34 PM

Yeah this is called an "anonymous" or "inner" class. It is a way for you to define a class within another function. Notice that it really has no name.

What this code is doing is creating a new instance of Clicklistener, which has no name and returns its address to "addClickListener" to be attached to the button. Typically you will see this method used for connecting listeners to controls. Especially mouse listeners.

Have you ever created an object and said "class blah implements ActionListener" at the top of your class? Then went ahead and did an actionPerformed method in your class and attached it to the button using "this" keyword? Well you could do the same thing by doing...

button.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent ae) {  
         // code that executes when button is pressed
       }
});



This creates an anonymous actionlistener anonymous object with no name, implements the interface ActionListener and thus overrides its actionPerformed method. It then returns the listener's address back to addActionListener to be attached to the button.

You can see other examples of anonymous inner classes for attaching to events etc at the URL below...

Anonymous Inner Classes in Java

I hope this helps.

"At DIC we be anonymous inner code ninjas! No classes for us, we are naturally kick ass!" :snap:

This post has been edited by Martyr2: 24 February 2008 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 c0mrade  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Re: Can someone explain this code? (its short i promise)

Posted 25 February 2008 - 07:25 PM

Thanks, that was a great explanation.

So the "new actionPreformed() {/*method*/}" syntax is specific to inline classes? or are there other uses...

And does "actionPreformed()" have to be an interface?

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1