Java Programming - Declarations and Access Control - Discussion

18. 

Which is a valid declaration within an interface?

[A]. public static short stop = 23;
[B]. protected short stop = 23;
[C]. transient short stop = 23;
[D]. final void madness(short stop);

Answer: Option A

Explanation:

(A) is valid interface declarations.

(B) and (C) are incorrect because interface variables cannot be either protected or transient. (D) is incorrect because interface methods cannot be final or static.


Bharath said: (Oct 16, 2010)  
public static short stop=23; is given as correct answer but how come its possible, interface does not accept static modifiers?

Hariom Singh said: (Aug 3, 2011)  
What is the working of join method in threading ?

Pawan Patidar said: (Nov 23, 2011)  
public static short stop=23; is given as correct answer but how come its possible, interface does not accept static modifiers?

Prabhat Kumar said: (Dec 2, 2011)  
Option A is also wrong, because static methods r not allowed in interfaces.

Google said: (Apr 14, 2013)  
Public static short stop=23; is given as correct answer but how come its possible, interface does not accept static modifier?

Mitesh Jain(Ujjain) said: (Aug 20, 2013)  
The answer(A) is right, because data member of interface can be static, final and public. Yes, Interface and classes can't be static. And interface's methods also can't be static. But data member is possible.

Gowtham said: (Dec 13, 2013)  
Yeah static methods are not allowed then how?

Nirali Khunt said: (Aug 30, 2014)  
Interface can not initialize the variable so it is not possible.

Vishnu Kumar Ravula said: (Mar 2, 2016)  
How can you access a static variable in interface using the its name without reference ?

Vijay said: (Aug 1, 2017)  
Interface by default all the variables are " PUBLIC STATIC FINAL " and all the methods are " PUBLIC and ABSTRACT ".

I hope it is clear now.

Alok S said: (Apr 15, 2019)  
Inside interface any variable you declare.

i.e
By default public static final ..and if u declare any methods it also public and Abstract.
Eg- interface Inf
{
Public static final int a=6;//variable decl
Public abstract m1 ();//decl of method
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.