I created a brand new Android application in Android Studio 0.3.6 with a LoginActivity.java that was created for me. When I view the onCreate
function I see the following code:
But when I click on the highlighted (view)-> {
code Android Studio displays what I would have expected to see:
findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attemptLogin();
}
});
My questions
An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class. Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.
An anonymous inner class is an inner class which is declared without any class name at all. In other words, a nameless inner class is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the same as the class name.
Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.
It's doing code folding similar to Java 8 lambda expressions (see the docs) even if your code isn't actually compiled with Java 8; it's just a convenience for a more compact display.
If you want to use them yourself, you can use Android Studio with Java 1.7 as a compile language, but you'll need to be using build tools v19 or later, and if you use certain language features, it will only run on phones with Kit Kat or later. Lambda functions won't get you into trouble, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With