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.
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) {
...
}
...
}
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.