It is a class in Java which represents an internet Protocol (IP) address. An instance of an InetAddress consists of an IP address and possibly corresponding hostname.
The Class represents an Internet address as Two fields:
1- Host name(The String)=Contain the name of the Host.
2- Address(an int)= The 32 bit IP address.
This fields are not public, so we cant Access them directly.
There is not public constructor in InetAddress class, but it has 3 static methods that returns suitably initialized InetAddress Objects.
public String getHostName()
public byte[] getAddress()
public String getHostAddress()
Factory methods are merely a convention whereby static methods in a class return an instance of that class. The InetAddress class has no visible constructors. In InetAddress the 3 factory methods getLocalHost, getByName, getAllByName can be used to create instances of InetAddress.
Yes. Using the method InetAddress.getByName( String name) for instance.
The InetAddress.getLocalHost() method will return an InetAddress object with your local address. You can get the data with a call to InetAddress.getLocalHost().getAddress() to return a byte array in which each byte refers to an octet of your IP address. Note that this will often not be useful to people connecting to the internet through a router. This will get your local network IP address and not the address that the rest of the world would see. For that you should look towards using platform-specific workarounds (ex: calling and parsing the ipconfig command in Windows).