Why is that a variable used in an Interface is PUBLIC STATIC FINAL? Why "static" in particular?
|
I wish to know is there any way in which I can make it compulsory for the implementer class to declare the objects handles/primitives as they do with methods.
for e.g.:
public interface ...
|
I'm having problem understanding an assignment in Java. Basicly we are writing an interpreter and should be simple. I have not used Java for over 2 years so nearly all my ... |
Hi ppl, I read in 2-3 books that variables declared in interface are implicitly final,static and classes implementing it cant change its value. But i changed the value of variable which ... |
Hi, I know that multiple inheritance is not allowed in java. I like to know what will happen for the following scenario: There are two interfaces InterfaceA and InterfaceB. Both these interfaces have a variable named 'i',and 'i' takes the value of 10 in InterfaceA and 20 in InterfaceB. I am implementing these two interfaces in a class say Class1. If ... |
Hi, I know the variables in interface are public static final by default and a top level interface should also be public. Now my question is to use interface only declare/use constants (not any methods) we can use it from anywhere by using interfacename.variable, but will there be any situation where we want to implement that interface just to use final ... |
This may have been a trick question. Your prospective employer may have been trying to find out what depth of trivia you know about the language. All variables declared in an interface are static and final by definition. Given that, can you tell us whether or not you think it is a good practice? |
|
Hi All. I have a performance related question regarding interface variables. Consider the following interface... interface I { // First case int i=0; } Since all variables declared in an interface are implicitly 'public static final', is it better to specify this explicitly as in the following? interface I { // Second case public static final int i=0; } My hunch ... |
|
an "interface" is intended to be just that - an interface. now the question is, an interface to what? ans. a class(abstraction of the object), and so the class must specify what interfaces it implements. and the developer can go ahead and use the particular interface to utilise the class. and that, in a way, explains why the methods are public. ... |
Originally posted by Rob Prime: Interfaces do not have any implementation details. Since instance and static variables are part of the implementation, they cannot be part of interfaces. Hence the only "variables" that can be part of interfaces are actually constants - static and final. And everything in an interface is public. |
interface Interfaceable { StringBuffer BUF = new StringBuffer ("A") ; StringBuilder BLD = new StringBuilder("C") ; String S = "E" ; Integer INT = new Integer(1) ; } public class GBTest implements Interfaceable { public static void main (String[] args) { BUF.append("B") ; System.out.println (BUF) ; // AB BLD.append("D") ; System.out.println (BLD) ; // CD // S = S + "F" ... |
|
public class TestKK { public static void main(String[] args) { System.out.println(J.i); System.out.println(K.j); } static int out(String s, int i) { System.out.println(s + "=" + i); return i; } } interface I { int i = 10; int ii = TestKK.out("ii", 2); } interface J extends I { int j = TestKK.out("j", 3); int jj = TestKK.out("jj", 4); } interface K extends ... |
Interfaces only describe behaviour, not state. Instance variables are part of an object's state. Hence they are not allowed in interfaces. Instead of giving compiler errors when you add a public field to an interface without explicitly making it static, the language creators thought that making them implicitly static was a better idea. |
Why is it allowed to override interface variables. Interfaces are meant to be followed as it is, then why a class is allowed to override the variable from an interface. interface I { int staticVar=50; } class A implements I { static int staticVar=10; // is this overriding the static var from I ? } public class MyClass extends A{ public ... |
interface i1{ void add(); int i = 5; } interface i2 extends i1 { void add(); int i = 10; } class C implements i2{ public void add(){ System.out.println(i); //Basically i want to ask , which "i" it refers to ? is it i2's "i" or i1's "i" and why ? .. and i guess 8 bytes were used to hold ... |
|
Yes you are right....absolutely actually i am taking constants as a subset of variables thatswhy i am saying that. i got the difference between me and javacompiler. Also Thanks to all of you....resolving my confusions and showing your patience while giving me answers. Edited by: j2ee_ubuntu on Sep 22, 2008 4:06 AM |
|
Test is type compatible with A (because it implements A). Therefore the static variable name can be used without a type qualifier. Don't do this though. It's a bad idea to use static interface fields and can always be avoided. Oh and if you mean: how does the variable get a value when all the values reference other variables? Because of ... |
interface A { public void show(); int var = 10; } interface B { public void show(); int var = 10; } public class AB implements A, B { public void show() { System.out.println("We got It"); System.out.println("Value is "+var);// will give an Error System.out.println("Value is "+A.var);//OR B.var } public static void main(String args[]) { |
Thank you for your quick reply. What i can make out is that Interfaces are abstract, so they cannot be instantiated. So making variables static would enable us to refer to the variables using the name of the Interface. The methods of an Interface cannot be static because they cannot be shared and their implementation is meant to be specific to ... |
|