Results 1 to 5 of 5
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why can't interfaces have non-constant variables?

    Why are interfaces allowed to have constant fields only?

    I mean what would be the motivation/reasoning of the Java language designers to make it illegal for interfaces to have non-constant fields/variables?
    Last edited by Zarah; 05-20-2016 at 08:02 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can't interfaces have non-constant variables?

    Probably because it is orthogonal to the purpose of interfaces. To permit non final fields would introduce state into the interfaces.
    An interface specifies what to implement, but now how to implement it. Non final fields would introduce some of the how. Keep in
    mind that final static fields can be objects which can indirectly reference other objects (the latter of which can change).
    But this could be dangerous. Consider the following (extremely contrived):

    Java Code:
    import java.util.Arrays;
    
    public class InterfaceDemo {
    
       public static void main(String[] args) {
          Fiddle1 f1 = new Fiddle1();
          f1.init();
          f1.FiddleWithMyArray();
          Fiddle2 f2 = new Fiddle2();
          f2.init();
          f2.FiddleWithMyArray();
          f1.FiddleWithMyArray(); //I already initialized it
          // hey, what jerk fiddled with my array
       }
    }
    
    interface BadIdea {
       int [] a = new int[2];
       public void FiddleWithMyArray();
    }
    
    class Fiddle1 implements BadIdea {
       public void init() {
       a[0] = 20;
       a[1] = 10;
       }
       public void FiddleWithMyArray() {
          System.out.println(Arrays.toString(a));
       }
    }
    class Fiddle2 implements BadIdea {
       public void init() {
          a[0] = 30;
          a[1] = 50;
       }
       public void FiddleWithMyArray() {
          System.out.println(Arrays.toString(a));
       }
    }
    Regards,
    Jim
    Last edited by jim829; 05-20-2016 at 11:05 PM. Reason: improved wording
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    May 2016
    Location
    Brazil
    Posts
    16
    Rep Power
    0

    Default Re: Why can't interfaces have non-constant variables?

    @Jim: Can you provide some words about your example?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can't interfaces have non-constant variables?

    The example simply illustrates that different implementations of the same interface could effectively interfere with each other
    if not careful (when using final static fields). Interfaces are permitted to have final static fields. However, in the case of the
    array 'a', the array reference assigned to 'a' cannot change, but the array contents may. So the user of f1 can't depend on the
    previous initialization of the array since f2 changed it. Final static fields are shared across all instances of the class which implements
    the interface.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    May 2016
    Location
    Brazil
    Posts
    16
    Rep Power
    0

    Default Re: Why can't interfaces have non-constant variables?

    Nice, understood it, thanks!

Similar Threads

  1. constant in object ??
    By dojob in forum New To Java
    Replies: 6
    Last Post: 09-17-2013, 08:02 PM
  2. Cannot use as static constant
    By KiranA in forum Eclipse
    Replies: 1
    Last Post: 12-01-2011, 09:19 AM
  3. How To Store Variables In A Text File That Aren't Constant
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 08-10-2011, 11:56 PM
  4. Constant Variables
    By harshakantha in forum New To Java
    Replies: 3
    Last Post: 06-13-2011, 07:51 AM
  5. constant variables questions
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 05:34 AM

Posting Permissions

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