ant2009
ant2009

Reputation: 22486

create an anonymous class pass in the constructor arguments and implement its interface

java version "1.7.0_45"

Hello,

class with constructor arguments that has an interface that will be implemented in the client

public class SInvitationListenerImp {
    public interface MUCRoomListener {
        void onInvitationReceived(String roomName, String inviter, String reason, String password, String message);
    }

    public SInvitationListenerImp(MUCRoomListener roomListenerEvent, int clientConnection) {
        mMUCRoomListener = roomListenerEvent;
        mClientConnection = clientConnection;
    }
}

And in my client could I would like to create a anonymous class pass in the constructor arguments and implement the interface.

However, this doesn't work with the arguements

new SInvitationListenerImp(MainActivity.this, connection).MUCRoomListener() {
            @Override
            public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {

            }
        };

This works without the constructor arguments. However, the arguments are needed (So this woudn't work for my needs)

new SInvitationListenerImp.MUCRoomListener() {
      @Override
          public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
      }
};

Is this possible to pass arguments to the constructor and implement the interface?

Many thanks for any suggestions

UPDATE results in '.' or ')' expected:

new SInvitationListenerImp(new SInvitationListenerImp.MUCRoomListener() {
        @Override
        public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
        }
    } MainActivity.this, connection);

Upvotes: 0

Views: 54

Answers (1)

Codebender
Codebender

Reputation: 14471

Your syntax is wrong... You probably need something like this,

new SInvitationListenerImp(new SInvitationListenerImp.MUCRoomListener() {
            @Override
            public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
                // Interface method body.
            }

        }, connection);

Upvotes: 2

Related Questions