
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Interface Variables are Static and Final by Default in Java
In Java, interfaces are used to achieve abstraction and multiple inheritance. They can contain methods and variables, but there are specific rules about how those members should behave.
For example, all variables declared in an interface are public, static, and final by default, even if you don't use these keywords while defining variables. To understand the reason behind it, we first need to understand what static and final mean in Java.
What is a Static Variable in Java?
The static variables are defined using the static keyword. These variables belong to the class rather than to any specific object, which means it is shared among all instances of that class and can be accessed without creating any object.
Example
In this example, the iterator is a static variable, and its value is shared across all instances. The constructor of the class increments the static variable each time an object is created.
class Counting { // static variable static int iterator = 0; // Constructor Counting() { iterator++; } // to display current value of 'iterator' void showIteration() { System.out.println("Count: " + iterator); } } public class Example { public static void main(String[] args) { // Creating instances new Counting(); new Counting(); new Counting(); new Counting().showIteration(); } }
On running the above Java program, you will get the following output:
Count: 4
What is a Final Variable?
A final variable is a constant. Therefore, once it is assigned a value, it cannot be changed. The final keyword is used in the variable declaration before the type.
Example
In this example, the MIN_AGE is a final variable. It cannot be reassigned after it has been initialized.
public class Example { // final variable final int MIN_AGE = 18; void printAge() { // Give a compile-time error MIN_AGE = 17; System.out.println("Minimum age: " + MIN_AGE); } }
On running, the above Java program will show the following error:
Example.java:7: error: cannot assign a value to final variable MIN_AGE MIN_AGE = 17; ^ 1 error
Interface Variables are Static and Final in Java
The list given below explains the reason why variables declared inside an interface is static and final by default:
- Interface variables are static because Java interfaces cannot be instantiated. Therefore, the value of the variable must be assigned in a static context so that it can be accessed using the name of an interface.
- The final non-access modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned. Otherwise, multiple implementations could have different copies and their behavior might be affected in a negative way.
Template of Interface in Java
interface interfaceName{ // Any number of final, static variables datatype variableName = value; // Any number of abstract method declarations returntype methodName(list of parameters or no parameters); }
Example: Interface with Static and Final Variables
The variables inside the Voter interface are public static final by default. If you try to modify them, the compiler will throw an error:
interface Voter { // default public static final int MIN_AGE = 18; String NATIONALITY = "Indian"; } public class Citizen implements Voter { void display() { System.out.println("Minimum Age: " + MIN_AGE); System.out.println("Nationality: " + NATIONALITY); } public static void main(String[] args) { Citizen c = new Citizen(); c.display(); } }
On running, the above Java program will show the following result:
Minimum Age: 18 Nationality: Indian