Since static fields objects are created at class level(and common to all objects), Is it static import on particular fields and inheriting(using implements) all the fields will create same amount of memory?
For Example In this below propgram how many MyOwn objects are created?
class MyOwn{}
public interface ConstantIfc {
public final static MyOwn REF = new MyOwn();
}
class A implements ConstantIfc {}
class B implements ConstantIfc {}
public class c {
public static void main(String... arg) {
A refA = new A();
B refB = new B();
}
}
If it is same how final constant class for static import is better than constant interface?
Update:
I understood that It is better to avoid Inherittance for constants. Improperly leveraging implementation inheritance often leads to inflexible design. So we can better go for static import of class/interface. but still Interfaces are an abstraction, and to remain abstract, they should not contain implementation details (including constant variables.) Interfaces also are often used to describe a public API, in which implementation details do not belong. For this reason it makes sense to put constant data into a class, rather than an interface. Thanks robjb.
In the example you give there’s only one
MyOwnobject that gets created. It is created the first time your program loads theConstantIfcinterface.Even if you used static imports the answer will still be the same. static imports are not supposed to be performance-wise better than constants-in-interfaces. They were introduced into the language for the purpose of avoiding interfaces which all they do is define constants as never used as interfaces (no variable is typed with the them) and thus induce confusion.