The InetAddress class has no visible constructors. To create an InetAddress object, you have to use one of the available factory methods. Factory methods are merely a convention whereby static methods in a class return an instance of that class. This is done in lieu of overloading a constructor with various parameter lists when having unique method names makes the results much clearer. In the case of InetAddress, the three methods getLocalHost(), getByName(), and getAllByName() can be used to create instances of InetAddress. These methods are shown here:
static InetAddress getLocalHost( )
static InetAddress getByName(String hostName)
static InetAddress[ ] getAllByName(String hostName)
The getLocalHost( ) method simply returns the InetAddress object that represents the local host. The getByName( ) method returns an InetAddress for a host name passed to it. If these methods are unable to resolve the host name, they throw an UnknownHostException.
On the Internet, it is common for a single name to be used to represent several machines. In the world of web servers, this is one way to provide some degree of scaling. The getAllByName( ) factory method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. It will also throw an UnknownHostException if it can't resolve the name to at least one address.
The following example prints the addresses and names of the local machine and two well-known Internet web sites:
// Demonstrate InetAddress.
public static void main(String args) throws
InetAddress Address = InetAddress.getLocalHost();
Address = InetAddress.getByName("starwave.com");
InetAddress SW = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
Here is the output produced by this program. (Of course, the output you see will be slightly different.)
This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".
1. It is showing error in SW??
kindly tell, how can I correct this
View Tutorial By: Jeet singh at 2016-03-14 20:09:10
2. HI, nice example you may refer following one also
View Tutorial By: Sreekanth Gde at 2014-11-25 16:35:24
3. How to display the ARP Mapping Table of a given host ?
View Tutorial By: Hala at 2013-11-15 22:42:36
5. nice 1... can u plz provide some code to work with ICMP ping
View Tutorial By: ashu at 2012-04-02 05:14:14
8. HI all i would like to send the result of " InetAddress Address = InetAddress.getLocalHost(); &
View Tutorial By: FLORIAN at 2011-09-14 09:04:42
9. Does anybody know how to force using IPv6 by my laptop??
I connect my computer to another one
View Tutorial By: Marcin at 2011-05-17 05:24:51
10. its really good work. and i need more documentation on it. but tats cool post lol
View Tutorial By: Rajesh at 2011-03-01 04:45:52
12. @Vaibhavn if you use an IP instead of a host name it works too!
View Tutorial By: RDMA at 2011-01-04 14:30:15
13. I am getting
Exception in thread "main" ja
View Tutorial By: GSA at 2010-11-23 02:36:26
15. it shows error
View Tutorial By: vamsi at 2010-05-14 23:09:03
17. Would be great if you also showed how to manipulate the output too.
View Tutorial By: Sparx at 2010-01-05 13:51:56
18. Can you give me an example to create InetAddress from IP address like 22.214.171.124
View Tutorial By: Vaibhav at 2009-12-15 23:37:49
Most Viewed Articles (in Java )
Latest Articles (in Java)
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews