3

Hello Stack Overflow Community. I am currently working on an Android VoIP application, which uses WebRTC and therefore I have been trying to comprehend the source code which I found on GitHub. So far, I have been able to reproduce and understand a bit of the source code I found, but currently I stumbled across a problem with the implementation of the SignalingClient. To further understand my problem I think it is appropriate to be more detailed and ask the question more precisely:

How to access all members in the current activity, without declaring an object of it from a class, through one instance method, which is defined in the same other class?

I think pointing to it in the source code is also good to understand the question. I have shortened the code so it fits the question as close as possible.

SignalingClient.java

class SignallingClient {
    private static SignallingClient instance;
    private String roomName = null;
    private Socket socket;
    boolean isChannelReady = false;
    boolean isInitiator = false;
    boolean isStarted = false;
    private SignalingInterface callback;
...
 public static SignallingClient getInstance() {
        if (instance == null) {
            instance = new SignallingClient();
        }
        if (instance.roomName == null) {
            //set the room name here
            instance.roomName = "vivek17";
        }
        return instance;
 }

 public void init(SignalingInterface signalingInterface) {
...
 }
...
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener, SignallingClient.SignalingInterface {
...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, ALL_PERMISSIONS_CODE);
        } else {
            // all permissions already granted
            start();
        }
    }
...
 public void start() {
        // keep screen on
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        initViews();
        initVideos();
        getIceServers();

        SignallingClient.getInstance().init(this); //Here is my lack of understanding/reproducing/comprehending, 
                                                   //because at this point I have access to all members(variables) 
                                                   //in the SignalingClient.java class : boolean isChannelReady, 
                                                   //boolean isInitiator, boolean isStarted
                                                   //and also other methods which are public...
        //Shouldnt it be like this?
        /*
        SignalingClient obj = new SignalingClient();
        obj.getInstance();
        obj.init(this);
        */
...
 }
...
}

After experimenting around and looking up on the web [1][2][3][4] and many other related stackoverflow questions I noticed that this behaviour has something to do with the static modifier according to these sources [5][6][7] but also the fact that

class SignallingClient{...                        //SignallingClient
private static SignallingClient instance;...      //SignallingClient
public static SignallingClient getInstance() {... //SignallingClient

the class, the instance member and also the getInstance method all have/share the same "name" as we can see on the line comments above. Getting back to my question, I would really like to know how this works, or if this is a trick to access all possible members and methods (and other things I don't know yet) without declaring an object. I would really appreciate any answer followed by a good explanation to this topic as well as dropping some good sources/tutorials/university scripts etc. where I can read up on this. Thank you very much.

Fermit
  • 61
  • 6
  • Your question got flagged for moderation since you're a new poster. Feedback: great question... you provided a ton of support which shows you put in a lot of effort. I would suggest adding the tags "voip" and "webrtc" since I see a lot of other questions using this tags. I hope you get a great answer. I upvoted your question. – buzz3791 Apr 16 '20 at 19:56
  • Sir, I appreciate your feedback. Indeed, I added as much information as I could on this topic, but frankly I refused the idea of adding too much information that could go into the wrong direction. Thank you very much for your recommendation for the tags as well as your upvote, really helpful and appreciated! – Fermit Apr 16 '20 at 20:37
  • 1
    That's the singleton pattern. – Johannes Kuhn Apr 18 '20 at 08:08
  • Sir, that answered my question! Thank you very much for your answer! – Fermit Apr 18 '20 at 08:38

0 Answers0