Java Unnamed Objects

I've been learning coding for 2 months. I'm working in Java on the Android platform. I'm trying to understand unnamed objects better.

In reference to the 7th line of this example, "this.button2.setOnClickListener(new OnClickListener(){...});"

a) Is the OnClickListener an object? (I think it is, since the "new" keyword was used)

b) Can (and should) it be named?

c) How would it be named?

d) How would one refer to it in the future? Is it possible? Can it be modified during runtime, if needs be?

e) Would this work?: "this.button2.setOnClickListener(OnClickListener namedObject = new OnClickListener(){//methods go here});"

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main2);     
    this.button2 = (Button) this.findViewById(R.id.button2);    
    this.button2.setOnClickListener(new OnClickListener()   
    {
        public void onClick(View v) 
        {                       
            Intent i = new Intent(getApplicationContext(), Absolute.class);
            startActivity(i);
        }
    });
}

Answers


a) Yes, it is an object.

b) Yes, it can be named, but it does not necessarily need to be (if you won't need to refer to it in the future, or have some other way of referring to it).

c) You can do:

OnClickListener myOnClickListener = new OnClickListener() {
    // Contents of the OnClickListener go here...
}
this.button2.setOnClickListener(myOnClickListener);

d) If you had named it (as seen above), it can be referred to by the name. It can be referenced at runtime if you have a reference to it by invoking any mutator methods on the object.

e) That syntax is not valid. See (c) for the proper way to make this call.


OnClickListener is, as you suspect, an object and is referred to as an Anonymous Class/Object. It does not need to be named because it is being used as a 'callback' for the object button2. What this means is that you are handing button2 an object, which button2 will delegate some work two. In this case, that work is everything that happens when the button is clicked.

Thus, when the button is clicked, it will call the object that you are setting here. In order to function, the 'onClick' method has to be defined, which you do in your code sample. Because that method is there, button2 is able to call it, and execute the code you provide - in this case creating an Intent object and starting an activity.

Because it's declared inside a function call, as soon as that function call is complete, it goes out of scope and you cannot reference it. There is no particular reason to name it for that reason (button2 will have an internal name which you don't have access to nor care about). If you declare the class outside the function call, you can name it and refer to it later... but typically this isn't needed.

I think that it can be modified during runtime, but you should avoid this complexity unless necessary.


This is java's anonymous class. You don't need to name it, of course you can name it, but if the class is used only once, you can use anonymous class for convenience. New onclicklistener() means that this anonymous class is derived from onclicklistener. You may also want to implement some interfaces in the anonymous class.


a. OnClickListener is a type (abstract class/interface), which happened to have a method that needs to be implemented. In this case you simply implemented the missing method "inline" b. It can be named. But there is no such thing as it should be. It is a matter of choice. If you intend to reuse the same object multiple times, you can name it.

c. Either by implementing inline like:

OnClickListener ocl = new OnClickListener() {
    public void onClick() {
       ...
    }
}

OR

public class MyOnClickListener extends OnClickListener {
    public void onClick() {
       ...
    }
}
OnClickListener ocl = new MyOnClickListener()

I assumed OnClickListener is an abstract class here. If it is an interface, you simply change the word extends to implements. The rest remains the same.

d. If you assign it to a variable, you can refer as such. You can't refer to the implementation if you implement it inline.

e. Assigning variable in the argument is not valid.


Need Your Help

Optimal Oracle SQL Query to complete group-by on multiple columns in single table containing ~ 7,000,000 records

sql oracle group-by query-optimization

I am a SQL Novice in need of some advice. What is the most efficient (fastest running query) way to do the following-