0

So in Java it's possible to instantiate an interface without creating an explicit class

public interface Foo {
  public void OnNotify()
}

Say I do the following somewhere else, say in a method Subscribe

public void Subscribe()
{
  final int someInt = 5;

  Foo bar = new Foo() {
    final int value = someInt;

    @Override
    public void OnNotify()
    {
        Log.d("Debug", "You are being notified that I hold the value " + value);
    }
  }

  someObject.AddSubscription(bar);
}

This is used extensively in Android for setting listeners to events.

Why is this possible, and does this kind of instantiation have a special name? Is this related to lambda functions in some way perhaps?

And why do I need to make a 'final' variable if I want to give it to this instantiated interface to hold. Say for example I wanted to pass the current iteration 'i' of a for loop to identify what index of an array a subscription references. I need to declare a final variable to hold 'i', and then pass it into the instantiated interface.

Edit: I'm still asking why I can instantiate an interface without making a class first, and what it's called. Not knowing what this is, there's no way I could have found the duplicate question, which doesn't cover what a Java anonymous class is.

Magic Marbles
  • 403
  • 2
  • 5
  • 15

1 Answers1

0

Why is this possible, and does this kind of instantiation have a special name? Is this related to lambda functions in some way perhaps?

  1. I don't know of any "special name" for this. I generally refer to it as a "interface implementation declaration." That's just me though, and maybe that's not accurate or correct.
  2. I believe they are not, because aside from syntax differences, I look to lambdas as a method-class like structure, and this form is overriding methods from an object type. When passing an interface object, you're passing reference of a type.

And why do I need to make a 'final' variable if I want to give it to this instantiated interface to hold.

The way I have looked at it, and understood it to be, is because this declaration inline and not in a separate class doesn't quite change what an Interface still is. In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types Link

An interface is required to have a constant, and to me final is a keyword that works similarly to the C/C++ keyword const in that it isn't holding the value but a reference to the type that you've declared. These values are immutable, preventing any copies and changes, as they only contain a reference to the location of the value.

So, overall, I believe that an interface declaration like how you illustrate is not working as declaring an instance of the actual interface class, but rather it's creating an object of that type and that object is a new location in memory that holds references to these methods and members (like a class).

Hope this helps some. I don't normally like to volunteer answers to something I am not 100% sure on, and wish this was a comment more (but lack the rep still for that), but hopefully it helps clear some things.

You can read the Java Language Specification here for more information on Interfaces too. Hope that helps.

Yoshi_64
  • 306
  • 2
  • 9