sockets Getting started with sockets How to instantiate a socket class object


Example

Instantiating a socket can be done in various ways.

  1. by 2 line declaration & instantiation:

    First we need to define a variable which will hold a Socket class object:

    Socket socket;
    

    then we can create a Socket class object:

    socket = new Socket();
    
  2. We can also make a one line definition & instantiation:

    Socket  socket = new Socket();
    

both ways will create an unconnected socket.

We can use other parameterized constructors to instantiate connected or unconnected socket class object:

For details see class doc specs:

https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html