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);}
}
}
@Tom has answered your direct question. Basically, you cannot declare instance-level attributes (or class-level variables) in an interface. Instance level attributes must be declared in a class.
If you want multiple classes to share the same attribute declarations, you could put them into an abstract superclass. But best practice is to declare the attributes as
privateand access them via getters and setters.There are a couple of other problems with your code:
You cannot declare a class and an interface with the same name in the same namespace. You have a class called
Formatthat implements an interface calledFormat, both declared in the namespace of theFormatBig. And even if you could (e.g. because they were declared in different namespaces), this is bad practice. Give the class and interface distinct names.You should use nested classes sparingly. It looks to me like you might have nested the
Formatclass and interface insideFormatBigas a convenience so that you only have to edit and compile one file. That is lazy. Alternatively, if you are doing it to organize your code, learn how to use Java packages.