944,027 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2697
  • Java RSS
Nov 10th, 2009
0

non-static method in a static context

Expand Post »
Hello All,
I am having some troubles with my code, which is:
Java Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Light Poster
Mehwish Shaikh is offline Offline
40 posts
since Apr 2008
Nov 10th, 2009
0
Re: non-static method in a static context
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!
Reputation Points: 10
Solved Threads: 0
Light Poster
Mehwish Shaikh is offline Offline
40 posts
since Apr 2008
Nov 10th, 2009
0
Re: non-static method in a static context
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.
Reputation Points: 11
Solved Threads: 3
Newbie Poster
santiagozky is offline Offline
18 posts
since Oct 2009
Nov 10th, 2009
0
Re: non-static method in a static context
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) ??
Reputation Points: 10
Solved Threads: 0
Light Poster
Mehwish Shaikh is offline Offline
40 posts
since Apr 2008
Nov 10th, 2009
0
Re: non-static method in a static context
make initCheck a static method. Do you know what the term static mean
in the context of programming?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,864 posts
since Dec 2008
Nov 11th, 2009
0
Re: non-static method in a static context
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Mehwish Shaikh is offline Offline
40 posts
since Apr 2008
Nov 2nd, 2010
0
Re: non-static method in a static context
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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maclord is offline Offline
1 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Print problem
Next Thread in Java Forum Timeline: get facebook profile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC