Results 1 to 6 of 6
  1. #1
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

    Java 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);
        }
    }
    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.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 07:32 AM.

  3. #3
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

    Default

    Hi ,
    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. 
    	}
    }
    i guess, i have communicated... anyways i will go thro' the link :)

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Basically i want to ask , which "i" it refers to ?
    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 instance

    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"
    }

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    i guess 8 bytes were used to hold i1's "i" and i2's "i" ... same question for fn add()
    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.

    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.

  6. #6
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 07:22 PM
  2. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  3. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 04:29 AM
  4. Using a variable in a SELECT FROM WHERE query
    By cplmckenzie in forum JDBC
    Replies: 12
    Last Post: 04-23-2008, 04:24 AM
  5. An interface query !!
    By ajaygargnsit in forum New To Java
    Replies: 3
    Last Post: 12-22-2007, 06:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •