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?