Results 1 to 6 of 6
Thread: Query abt interface variable
- 06-25-2011, 05:59 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 13
- Rep Power
- 0
Query abt interface variable
Java Code:interface i1{ void add(); int i = 5;// by default this variable is public static final } interface i2 extends i1 { void add(); int i = 10; // by default this variable is public static final } class C implements i2{ public void add(){ System.out.println(i); } }
I ran the above code , it was compiling ...
my query is , what abt the variable i and Fn add() ? will it get updated ?
or two two add() and variable i will present in memory?
Thanks in advance...
Gobi.S
Have a happy weekend
- 06-25-2011, 06:29 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Interfaces promise that a method will be defined, but they don't actually define the method - in other words they say what the method is called, but they don't say what the method is.
The code defines the add() method only once. In the C class.
----------
The i field is a bit different. The first interface (please use a capital letter for interfaces) declares it and gives it a value of 5. The second interface extends the first and declares the i field and gives it a value 10.
As with any static field the second declaration is said to "hide" the first. A little code should show that the add() method will use the value of the unhidden i (10) but the hidden i (and its value of 5) is still there and accessible.
I am compelled to add that ((i1)test).i is a monstrosity - and not just because the interface should have been named I1. The point is that i is static, so the proper, decent, appropriate and polite way to refer to it is using the interface: i1.iJava Code:class C implements i2{ public void add(){ System.out.println(i); } public static void main(String[] args) { C test = new C(); test.add(); System.out.println(i1.i); System.out.println(((i1)test).i); } }
------
If you find it confusing that a final variable should be assigned a value and then apparently used and found to have a different value, then don't write code like that.Last edited by pbrockway2; 06-25-2011 at 06:32 AM.
- 06-25-2011, 06:46 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 13
- Rep Power
- 0
Hi ,
i guess, i have communicated... anyways i will go thro' the link :)Java Code: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 i1's "i" and i2's "i" ... same question for fn add()... pls clarify if i am wrong. } }
- 06-25-2011, 07:09 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
It refers to the i declared in i2, since C implements i2. (See the code I posted where the value is printed.) It matters that i2 is a direct superinterface. For instanceBasically i want to ask , which "i" it refers to ?
Java Code:class C implements i2, [color=red]i1[/color] { public void add(){ System.out.println(i); // won't compile: "the field i is ambiguous" }
- 06-25-2011, 07:19 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I'm not an expert about this - so others should correct me - but I'm not sure that any particular number of bytes is demanded.i guess 8 bytes were used to hold i1's "i" and i2's "i" ... same question for fn add()
In any case the question you asked was about the values held by variables within methods: ie was about the Java language. This language has remarkably little (nothing?) to do with memory, bytes etc. Therein lies a lot of its simplicity.
The value of i within the method add() is most easily seen by writing a small main() method and running the code. Understanding why it has this value is best seen in the terms of the Java language (specifically what values static variables are defined to have when they are declared in multiple supertypes) rather than in terms of memory "tacked on" to the Java language.
- 06-25-2011, 04:27 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Dynamic variable name based on other variable
By nadissen in forum EclipseReplies: 4Last Post: 05-06-2011, 06:22 PM -
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
Interface variable to class
By zill in forum Advanced JavaReplies: 6Last Post: 10-11-2008, 03:29 AM -
Using a variable in a SELECT FROM WHERE query
By cplmckenzie in forum JDBCReplies: 12Last Post: 04-23-2008, 03:24 AM -
An interface query !!
By ajaygargnsit in forum New To JavaReplies: 3Last Post: 12-22-2007, 05:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks