This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Android and the fly likes interface and constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "interface and constructor" Watch "interface and constructor" New topic
Author

interface and constructor

Jeansonne Pierre
Greenhorn

Joined: Sep 11, 2011
Posts: 12
Hi, Im still very fresh in java
help needed
the question is within the coding comment
Please kindly advise and correct me if i'm wrong... thanks
*****************************
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TryActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pushButton = (Button)findViewById(R.id.push_button);

View.OnClickListener xx =new View.OnClickListener()
{
// as i know , the new keyword is for creating an object... View.OnClickListener() is a constructor... View.OnClickListener is a reference type
// the class View.OnClickListener is an interface
// the problem is an interface doesn't allow any object instantiated from it and that's why interface automatically don't have any any constructor implement in it own class
// but why the new View.OnClickListener() appear on above and creating an object with constructor... why???
// The API link is here http://developer.android.com/reference/android/view/View.OnClickListener.html

public void onClick(View v)
{
Intent intent = new Intent(TryActivity.this, Try2Activity.class);
startActivity(intent);
}

};

pushButton.setOnClickListener(xx);
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
It's possible to use this syntax ("new" with an interface) if you supply implementations of all methods required by the interface. This creates a so-called anonymous class (because it has no name). It's a shortcut to creating an actual class that implements the interface, and instantiating that. That's a feature of Java, not Android in particular.


Ping & DNS - my free Android networking tools app
Jeansonne Pierre
Greenhorn

Joined: Sep 11, 2011
Posts: 12
ooo okok.. i see i see... thanks
but where do the compiler find constructor View.OnClickListener() ?? is it a default constructor with onClick() method??
if i create it on this way as below, why must put the new keyword also?? can i code this without new keyword? why we need that anonymous class/object??
thanks
***************************
public class TryActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pushButton = (Button)findViewById(R.id.push_button);
pushButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TryActivity.this, Try2Activity.class);
startActivity(intent);
}
});
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
but where do the compiler find constructor View.OnClickListener() ? is it a default constructor with onClick() method?

Yes, the compiler inserts a default constructor into the newly created class.

if i create it on this way as below, why must put the new keyword also?

Because that's how you construct objects on Java.

can i code this without new keyword?

That's easy to test, no?

why we need that anonymous class/object?

You don't - nothing stops you from creating a regular, named class that implements the interface, and use that instead of an anonymous class. This is merely a convenient shortcut that lets you avoid that.
Jeansonne Pierre
Greenhorn

Joined: Sep 11, 2011
Posts: 12
thanks
one more question
i tried to change and modify but can't get the same result

question : how to code the same output with the different method which implements the View.OnClickListener without creating the anonymous class as below?
please show an example
thanks

***************************************
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class try1Activity extends Activity implements View.OnClickListener{


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pushButton = (Button)findViewById(R.id.push_button);
// View.OnClickListener xx = null;

// pushButton.setOnClickListener(xx);

}

@Override
public void onClick(View v) {
Intent intent = new Intent(try1Activity.this, try2Activity.class);
startActivity(intent);

}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
pushButton.setOnClickListener(this) should do the trick.
Jeansonne Pierre
Greenhorn

Joined: Sep 11, 2011
Posts: 12
Wao... thanks guru... it runs perfectly...
Sorry for disturb again... i got one more question... which is

Im trying to get understand on the "this" keyword
for my understanding, the "this" keyword at here refer to current object whose method is being called.
my question are:
1) current object = object type of View.OnClickListener ?
2) why the compiler will choose only the 1st onClick method instead of 2nd onClick?

---------------------------------------------------------------------------------------

public class RedActivity extends Activity implements View.OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button greenButton = (Button)findViewById(R.id.green_button);

greenButton.setOnClickListener(this);
}
//chosen by compiler
public void onClick(View v) {
Log.w("1st",""+this+"");
}

//ignore by compiler
public void onClick(View v, int a) {
Log.w("2nd",""+this+"");
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
1) current object = object type of View.OnClickListener ?

Yes, because RedActivity -which is what "this" refers to- implements View.OnClickListener.

2) why the compiler will choose only the 1st onClick method instead of 2nd onClick?

Because that's the one that fulfills the View.OnClickListener interface.
Jeansonne Pierre
Greenhorn

Joined: Sep 11, 2011
Posts: 12
the "this" keyword refer to current object whose method is being called


in the situation above
'this' refer to the object of type View.OnClickListener
then
whose method is being called... refer to.....???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
I don't think I understand the question - the View.OnClickListener's method is called.
Jeansonne Pierre
Greenhorn

Joined: Sep 11, 2011
Posts: 12
i got it i got it....
thanks for your help....
i've learn a lot... thanks again...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface and constructor