I am writing a simple Interface class as below. I need to make only one of its methods available to the user. The user using this class needs only the return value of this particular method (checkLink
). Therefore I have declared this method alone as protected so it is visible only to the user calling the Iface
class. Since the rest of the methods in the Class are to be used within the class they have been declared as private. I added Static keyword as these methods are sort of "stateless". I used static final keyword for the class variables as I wanted something like a C like #define.
Since I am C programmer I am still new to the Java world. Do take a look at the skeleton of my code and tell me if the declarations made are right in conveying the meaning.
package com.prog.Test;
import java.net.*;
import java.io.*;
public class Iface{
private static final int MSG_OK = 0x01;
private static final int MSG_NOK = 0x00;
private static final int MSG_FAIL = 0xFF;
private static byte[] getBytesFromUrl(URL link) throws IOException {
......
return bytes;
}
private static int[] msg_header(byte[] msg) throws Exception
{
int len = msg.length;
System.out.println("len ="+len);
int[] headerMsg =new int [3];
headerMsg[0]= MSG_OK;
......
return headerMsg;
}
private static byte[] Tobyte(int[]src) {
....
}
protected static int checkLink (URL url ) throws IOException {
byte[] rbuffer = null;
byte[] hdr = null;
int status = -1;
try{
rbuffer = getBytesFromUrl(url);
..
..
hdr = Tobyte(msg_header(rbuffer));
...
...
status = 0;
}catch (Exception e开发者_开发问答) {
System.err.println("Exception: " + e);
}
return status;
}
}
So when I use this Iface
class in an Application, I call it this way:
public class Trial {
public static void main (String [] args ) throws IOException {
URL url = new URL("http://localhost:8000/myfile");
System.out.println("Calling Iface");
int retval = Iface.checkLink(url);
System.out.println("retval ="+retval);
}
}
I also tried this (after removing the static
keyword in checkLink()
method):
public class Trial {
public static void main (String [] args ) throws IOException {
URL url = new URL("http://localhost:8000/myfile");
System.out.println("Calling Iface");
Iface callInterface;
int retval = callInterface.checkLink(url);
System.out.println("retval ="+retval);
}
}
Is there a difference?
Calling static methods via an instance and via the class are functionally equivalent in Java. The following:
Iface.checkLink(<url>);
is functionally equivalent to
Iface foo = new Iface(); // or Iface foo; but with the caveats below
foo.checkLink(<url>);
However, note that the latter is not recommended because it is confusing and unnecessary. It could be argued that it's only allowed because of a mistake by the Java language designers.
Caveat: In your example you're not initializing your Iface
variable. You can still call static methods via this variable, however you would likely run into issues down the line if you were using it for anything but static methods (which you shouldn't be doing in the first place!).
In fact, you should probably go so far as to make this class uninstantiable since it appears to be a utility class with nothing but static methods.
Technically, neither of the code examples you posted should work.
The protected
keyword tells Java to only allow classes to access that variable if they extend the Iface
class. You are not extending the class in your calling code, so neither approach to using the method should compile if the method is not public
.
Be aware that making the method static
means that callers will need to use the Iface
class directly, making it impossible to use class inheritance. So if you will ever want to have another class that extends Iface
and overrides the implementation of checkLinks
, you'd want to make the method non-static.
If the method is non-static, you'd want to use it like this:
Iface callInterface = new Iface();
int retval = callInterface.checkLink(url);
You may also want to do some research on the factory pattern and the dependency injection pattern, which give you more flexibility in determining which specific Iface
implementation you want to use.
Yes,
by doing this: Iface callInterface;
you are creating an instance of the class Iface
and therefore reserving some space in the memory.
Howver, you're still executing a static method, so you can't use any of the instance methods/attributes.
So in terms of execution, there won't be any difference. In terms of memory/garbage collection, there will.
Sure there's a difference.
Static methods are not members of the object, they belong to the class.
So if checkLink()
is static, then you should call it from the class itself: IFace.checkLink()
.
Calling it from a reference to an object will work, but logically it's not the right meaning. Because the method doesn't belong to the object, and won't work on the object's actual state.
I guess that your question would be mor accurate like "What does static mean?".
For me simply "static" means, that those variables are there only once per class, all others are in every object coming from that class.
精彩评论