In a socket programming code there was a line like this
InetAddress address = InetAddress.getLocalHost();
What's done here? If I create an object of InetAddress
isn't that would be like this?
InetAddress address = new InetAddress();
In a socket programming code there was a line like this
InetAddress address = InetAddress.getLocalHost();
What's done here? If I create an object of InetAddress
isn't that would be like this?
InetAddress address = new InetAddress();
InetAddress.getLocalHost()
is a public static
method of InetAddress
.
Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.
There are many cases where an instance is not created by you using a constructor but by a static method like this or by a factory.
As you can see in the documentation, there isn't an implemented constructor for InetAddress
.
Instead, you need to use some static
methods that return an InetAddress
, such as getLocalHost()
.