Java: why is declaration not sufficient in interface?
- by HH
Big class contains Format-interfcase and Format-class. The Format-class contains the methods and the interface has the values of the fields. I could have the fields in the class Format but the goal is with Interface. So do I just create dummy-vars to get the errors away, design issue or something ELSE?
KEY: Declaration VS Initialisation
Explain by the terms, why you have to init in interface.
What is the logic behind it?
To which kind of problems it leads the use of interface?
Sample Code having the init-interface-problem
import java.util.*;
import java.io.*;
public class FormatBig
{
private static class Format implements Format
{
private static long getSize(File f){return f.length();}
private static long getTime(File f){return f.lastModified();}
private static boolean isFile(File f){if(f.isFile()){return true;}}
private static boolean isBinary(File f){return Match.isBinary(f);}
private static char getType(File f){return Match.getTypes(f);}
private static String getPath(File f){return getNoErrPath(f);}
//Java API: isHidden, --- SYSTEM DEPENDED: toURI, toURL
Format(File f)
{
// PUZZLE 0: would Stack<Object> be easier?
size=getSize(f);
time=getTime(f);
isfile=isFile(f);
isBinary=isBinary(f);
type=getType(f);
path=getPath(f);
//PUZZLE 1: how can simplify the assignment?
values.push(size);
values.push(time);
values.push(isfile);
values.push(isBinary);
values.push(type);
values.push(path);
}
}
public static String getNoErrPath(File f)
{
try{return f.getCanonicalPath();
}catch(Exception e){e.printStackTrace();}
}
public static final interface Format
{
//ERR: IT REQUIRES "="
public long size;
public long time;
public boolean isFile=true; //ERROR goes away if I initialise wit DUMMY
public boolean isBinary;
public char type;
public String path;
Stack<Object> values=new Stack<Object>();
}
public static void main(String[] args)
{
Format fm=new Format(new File("."));
for(Object o:values){System.out.println(o);}
}
}