InetAddress class provides the object representation of IP address.

An object of InetAddressa class is used to represent the IP address of any system in the form of Object.

InetAddress is a Singleton class which provides factory methods to create and manage the object. There are no constructors for this class but static methods which returns instances of InetAddress class for general use.

  • Singleton class in java

    A singleton class is a class of which only one object is created in any condition. It is also known as factory class because it provides factory methods to create and manage the objects.


    how to create singleton class in java

    
    class Singleton {
    	private static Singleton single = null;
    
    	// make constructor private
    	private Singleton() {
    
    	}
    
    	// define the static factory method
    	public static Singleton getSingle() {
    		if (single == null) {
    			single = new Singleton();
    		}
    		return single;
    	}
    
    }


    Another class which contain main() method

    
    class First {
    	public static void main(String[] args) {
    		// Singleton s1=new Singleton();//error
    		Singleton s1 = Singleton.getSingle();
    		System.out.println(s1);
    		Singleton s2 = Singleton.getSingle();
    		System.out.println(s2);
    	}
    }

    we have create two object using getSingle() method but both object is same because it is Singleton class.

  • Methods of InetAddress Class

    Before using InetAddress class we have to import java.net package.

    Method Syntax Description
    public static InetAddress getLocalHost() throws UnknownHostException This method is used to find out the IP address of current machine in the form of object.
    public static InetAddress getByName(String host) throws UnknownHostException This method is used to find out the IP address of any system by using its host name.
    public static InetAddress[] getAllByName(String name) throws UnknownHostException This method is used to obtain all the IP address of the given host .
    public String getHostAddress() This method is used to return the IP address in the form of string.
    public String getHostName() This method is used to return the Host Name .

Example of finding out the IP of local machine.
drag_handle

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPLocal {
	public static void main(String[] args) {
		try {
			InetAddress local = InetAddress.getLocalHost();
			String address=local.getHostAddress();
			String hostName=local.getHostName();
			System.out.println("Local Host IP Address :"+address);
			System.out.println("Local Host Name :"+hostName);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}

Output:

Local Host IP Address :127.0.0.1
Local Host Name :durgesh-lappy
=============================
If the system is connected to internet then the output is :-
Local Host IP Address :192.168.43.155
Local Host Name :durgesh-lappy


Example of finding out the IP of any machine using host name.
drag_handle

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
public class IPAny {
	public static void main(String[] args) {
		try {
			System.out.println("Enter the host Name");
			Scanner ob = new Scanner(System.in);
			String host = ob.nextLine();
			InetAddress local = InetAddress.getByName(host);
			String address = local.getHostAddress();
			String hostName = local.getHostName();
			System.out.println("Local Host IP Address :" + address);
			System.out.println("Local Host Name :" + hostName);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}
}

Output:

Enter the host Name
localhost
Local Host IP Address :127.0.0.1
Local Host Name :localhost
======run again==========
Enter the host Name
www.google.com
Local Host IP Address :172.217.160.164
Local Host Name :www.google.com
======run again==========
Enter the host Name
www.onlyjavatech.com
Local Host IP Address :45.113.136.152
Local Host Name :www.onlyjavatech.com


Example of finding out the All IP of any machine using host name.
drag_handle

import java.net.InetAddress;
import java.util.Scanner;

public class IPAll {
	public static void main(String[] args) {
		try {
			System.out.println("Enter the host Name");
			Scanner ob = new Scanner(System.in);
			String host = ob.nextLine();
			InetAddress[] ads = InetAddress.getAllByName(host);
			for (InetAddress x : ads) {
				System.out.println("Address :" + x.getHostAddress());
				System.out.println("Host Name :" + x.getHostName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output:

Enter the host Name
www.google.com
Address :172.217.160.164
Host Name :www.google.com
Address :2404:6800:4009:80a:0:0:0:2004
Host Name :www.google.com

What next?

The next topic is Socket Programming in java

Share this page