non-static method in a static context

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

non-static method in a static context

 
0
  #1
24 Days Ago
Hello All,
I am having some troubles with my code, which is:
  1.  
  2. import java.net.*;
  3. import java.io.*;
  4.  
  5. class TestInet
  6. {
  7.  
  8. public static void main(String args[])
  9. {
  10. try{
  11.  
  12. InetAddress inet = InetAddress.getByName("72.14.203.106");
  13. InetAddress inet1 = InetAddress.getLocalHost();
  14. InetAddress inet2[] = InetAddress.getAllByName("www.google.com");
  15. //InetAddress inet3 = InetAddress.getHostName();
  16.  
  17. System.out.println ("Host of IP is[inet.getByName]: " + inet.getHostName());
  18. System.out.println("Localhost is[inet.getLocalHost]: " + inet1);
  19. for(int a = 1; a < inet2.length; a++)
  20. {
  21. System.out.println("IPs of google are[i.getAllByName]: " + inet2[a]);
  22. }
  23.  
  24. inetCheck();
  25. //System.out.println("Localhost is[inet.getHostName]: " + inet3);
  26. }
  27. catch(Exception e)
  28. {
  29. e.printStackTrace();
  30. }
  31.  
  32. }
  33.  
  34. private void inetCheck()
  35. {
  36. InetAddress inet3 = InetAddress.getHostName();
  37. System.out.println("Localhost is[inet.getHostName]: " + inet3);
  38. }
  39.  
  40. }

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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster
 
0
  #2
24 Days Ago
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: santiagozky is an unknown quantity at this point 
Solved Threads: 3
santiagozky santiagozky is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster
 
0
  #4
24 Days Ago
I hope I'm getting you correctly..
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) ??
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,262
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
24 Days Ago
make initCheck a static method. Do you know what the term static mean
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster
 
0
  #6
23 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

Tags
inetaddress, method, nonstatic, static

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC