0

As per this

In Java 7 or older version, interface could not have any variables. Interface could only have methods which should be public.

Is this statement correct?

As per above link

Though, in Java 8, you could have variables inside interface and all variables should be declared static and final

If its static and final then can we call it a variable?Or it should be constant?

Can't we declare String bh=""; in an interface of Java 7? I tried in android studio by disabling java 8 and I am able to declare that. I was asked by an interviewer same question that "Can we declare variables in Java 7" and my answer was "yes".

varmashrivastava
  • 432
  • 2
  • 6
  • 21
  • refere this:-https://stackoverflow.com/questions/2430756/why-are-interface-variables-static-and-final-by-default. – Mr. Roshan May 24 '18 at 04:58
  • yes you can create like 'String bh = "";' but it is a constant not variable – janith1024 May 24 '18 at 04:59
  • @janith1024 Does that mean that java 8 interface support non static and non final fields? – varmashrivastava May 24 '18 at 05:02
  • Java 8 also not support for the variables. It support for default methods and static methods – janith1024 May 24 '18 at 05:03
  • @janith1024 So that mean that in java 8 we cannot have variables.But i was told by interviewer that in java 8 we can have and its written in th link in question also..So is tht wrong? – varmashrivastava May 24 '18 at 05:06
  • @janith1024 please check updated question – varmashrivastava May 24 '18 at 05:10
  • in the question they mention final and static variable it means constants. According to my knowledge Java 8 doesn't change variables. please check the Mr. Roshans link you can get idea from that – janith1024 May 24 '18 at 05:11
  • Your interviewer is wrong. From very early age of Java, you can declare "variable" in interface, but that will be static final implicitly. Such rule didn't change in Java8 afaik – Adrian Shum May 24 '18 at 06:00

1 Answers1

2

From http://knowledgehills.com/java/java-interface-variables-default-methods.htm

We can declare variables in Java interfaces. By default, these are public, final and static. That is they are available at all places of the program, we can not change these values and lastly only one instance of these variables is created, it means all classes which implement this interface have only one copy of these variables in the memory.

In short these are the rules for interfaces:

Member variables

  • Can be only public and are by default.
  • By default are static and always static
  • By default are final and always final

Methods

  • Can be only public and are by default.

  • Can not be static

  • Can not be Final


Java 8 Form http://www.javahelps.com/2015/02/introduction-to-interface-with-java-8.html

Up to Java 1.7 version, all the methods declared in interfaces are public and abstract by default. Since Java 1.8, an interface can have default methods and static methods as well. Therefore, the updated rule

An interface can have default methods and static methods. Any other methods are implicitly public and abstract. All the fields declared in an interface are implicitly public, static and final constants.

Example:

interface Super {

      //An abstract method. By default it is public and abstract.

    void print();

     //Default method, introduced in Java 8.

    public default void doStuff() {
        System.out.println("Hello world");
     }       
      //Static method in interface, introduced in Java 8.

    public static void sayHello() {
        System.out.println("Hello");
     }
 }

Implementation:

class Base implements Super {

        // Override the abstract method.

        @Override
        public void print() {
            System.out.println("Base");
       }
}
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
Bellal Hossain
  • 240
  • 3
  • 8
  • This is what I knew..But my interviewer told me that before java 8 we cannot declare variables and in java 8 we can declare variables?Also doubt in ur first line "We can declare variables in Java interfaces. By default, these are public, final and static. "..But if they are final can we call them variable? – varmashrivastava May 24 '18 at 05:45
  • Yes you can call final variable. – Bellal Hossain May 24 '18 at 08:11