-2

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();
  • no, because the static method `InetAddress.getLocalHost();` returns value of type `InetAddress` – Sufiyan Ghori Feb 03 '15 at 14:11
  • 1
    possible duplicate of [Difference between Static methods and Instance methods](http://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods) – Ceiling Gecko Feb 03 '15 at 14:12
  • 6
    I'm voting to close this question as off-topic because the OP needs to have a minimal understanding of programming. – nalply Feb 03 '15 at 14:13
  • @nalply By the way, where in the rules is it said that a topicstarter has to have minimal understanding of programming? It seems to me the purpose of this particular question is to get that very minimal understanding, as the person is clearly very new to programming. Should we discourage such kind of questions? – gvlasov Feb 03 '15 at 14:21
  • 1
    @Suseika That's a topic for Meta. –  Feb 03 '15 at 14:25

2 Answers2

5

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.

0

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().

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28