| | |
non-static method in a static context
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hello All,
I am having some troubles with my code, which is:
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:
found:java.lang.string
required: java.lang.inetAddress (line 36)
The second thing I'm not getting is getByName("72.14.203.106"), 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.
Regards!
I am having some troubles with my code, which is:
Java Syntax (Toggle Plain Text)
import java.net.*; import java.io.*; class TestInet { public static void main(String args[]) { try{ InetAddress inet = InetAddress.getByName("72.14.203.106"); 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]); } inetCheck(); //System.out.println("Localhost is[inet.getHostName]: " + inet3); } catch(Exception e) { e.printStackTrace(); } } 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:
found:java.lang.string
required: java.lang.inetAddress (line 36)
The second thing I'm not getting is getByName("72.14.203.106"), 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.
Regards!
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 3
0
#3 24 Days Ago
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.
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.
0
#5 24 Days Ago
make initCheck a static method. Do you know what the term static mean
in the context of programming?
in the context of programming?
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
![]() |
Similar Threads
- non-static method cannot be referenced from a static context (Java)
- OOP Fatal error: Non-static method (PHP)
- Trouble with a static method (Java)
- non-static can't be referenced from a static context (Java)
- Non-Static method cannot be referanced from a Static context (Java)
- Non-static method can't be referenced from a static context (Java)
Other Threads in the Java Forum
- Previous Thread: Conversion error - I'm completely stumped!
- Next Thread: adding more then one element to a line in a JList
| Thread Tools | Search this Thread |






. Don't get irritated by the print statements, that was only for my own understanding.

