Hello All,
I am having some troubles with my code, which is:

import java.net.*;
import java.io.*;

class TestInet

	public static void main(String args[])

			InetAddress inet = InetAddress.getByName("");
			InetAddress inet1 = InetAddress.getLocalHost();
			InetAddress inet2[] = InetAddress.getAllByName("www.google.com");
			//InetAddress inet3 = InetAddress.getHostName();

			System.out.println ("Host of IP is[inet.getByName]: " + inet.getHostName());
			System.out.println("Localhost is[inet.getLocalHost]: " + inet1);
			for(int a = 1; a < inet2.length; a++)
				System.out.println("IPs of google are[i.getAllByName]: " +  inet2[a]);
			//System.out.println("Localhost is[inet.getHostName]: " + inet3);
		catch(Exception e)
	private void inetCheck()
		InetAddress inet3 = InetAddress.getHostName();
		System.out.println("Localhost is[inet.getHostName]: " + inet3);


Its giving three errors;
1)non-static method initCheck() cannot be referenced from static context (line 24)
2)non-static method getHostName() cannot be referenced from static context (line 36)
3)incompatible types:
required: java.lang.inetAddress (line 36)

The second thing I'm not getting is getByName(""), I thought it would return Host name, but it's giving me some irksome values like "tx-in-f106.1e100.net"
So what's this basically doing ??
Hope to see your help soon.

Recommended Answers

All 6 Replies

Oops I forgot to post some simple code :$. Don't get irritated by the print statements, that was only for my own understanding.
Thank you!

When you declare something as static it means that that 'stuff' belongs to the class itself and not to the objects generated by that class. You don't need (should not) to create an object to call an static method or access and static field (java.lang.Math.* methods are a good example of this).

Any other non-static thing should be accessed from an object (Object a a= new Thing(); a.method();).

This means that you cannot combine static and non-static things so easily. When main is executed is in a static context, so you cannot call a non static method (inetCheck) since it needs to be called from an object.

So you have 2 options.
Remove your code from main and put it in a non static context (a contructor por example) or modify inetCheck to be static.

I hope I'm getting you correctly..:S
OKay I'm making inetCheck() as static or making some constructor, but what about getHostName() which is a non static method and I am unable to call to it both from the main(static method) and the inetCheck()(non-static) ??

make initCheck a static method. Do you know what the term static mean
in the context of programming?

I have read about static a lot, but still need something about it :$
Well if I make inetCheck static, then I dont even need to make this method as main is already an static one.
What the problem is that I have to include the getHostName() which is a nonstatic method into my program.

I'm having a similar problem, but my methods are EJB methods and I'm trying to call them from a jsf-managed bean. How should I reference the EJB business methods from within the JSF managed-bean ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.