开发者

Java: why is declaration not sufficient in interface?

开发者 https://www.devze.com 2022-12-28 15:04 出处:网络
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

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

  1. Explain by the terms, why you have to init in interface.
  2. What is the logic behind it?
  3. 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){i开发者_如何学编程f(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);}
        }
}


Fields in interfaces are implicitly public final static.

static will mean that they are independent of instances. This is probably not what you want.

final means they need to be assigned exactly once during class initialisation, as the fields are also static. This usually means assigning in the declaration, but could use a static initialiser. If they were instance fields (non-static), then they would need to be assigned either in the declaration, in constructors (that implicitly or explicitly call super rather than a "this" constructor) or in an instance initialiser.

You should probably move instance fields into the implementing class, and mark them final.


@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 private and 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 Format that implements an interface called Format, both declared in the namespace of the FormatBig. 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 Format class and interface inside FormatBig as 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消