InetAdress class
no constructor
---------------------------------------------------
If a class does not have a constructor, there are three possible situations:
A: The members are all static (Math, Arrays, Collections)
B: Singleton Design Pattern
C: There is a static method in a class that returns an object of that class (InetAdress)
class Demo{ private Demo(); public static Demo getXxx(){ return new Demo();} }
Member methods of InetAddress:
public static InetAddress getByName(String host): Obtained according to the string representation of the host name or IP address
IP address.
package cn.itcast_01;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
//according to hostname
//InetAddress address = InetAddress.getByName("DESKTOP-0257CLH");
//According to IP address
InetAddress address = InetAddress.getByName("10.141.25.35");
//Get two things hostname, Ip address
String name = address.getHostName();
String ip = address.getHostAddress();
System.out.println(name+"------"+ip);
//DESKTOP-0257CLH------10.141.25.35
}
}