- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens if we does not initialize variables of an interface in java?
In Java final is the access modifier which can be used with a filed class and a method.
- When a method if final it cannot be overridden.
- When a variable is final its value cannot be modified further.
- When a class is finale it cannot be extended.
Declaring final variable without initialization
If you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don’t you will get a compilation error.
Example
In the following java program, we a have an interface with a public, static, final variable with name num and, a public, abstract method with name demo.
public interface MyInterface { public static final int num; public abstract void demo(); }
Compile time error
On compiling, the above program generates the following error.
Output
MyInterface.java:2: error: = expected public static final int num; ^ 1 error
- Related Articles
- What happens if we overload default methods of an interface in java?
- What happens if a class does not implement all the abstract methods of an interface in java?
- What happens if we define a concrete method in an interface in java?
- Can we create non static variables in an interface using java?
- What happens if the subclass does not override abstract methods in java?
- What kind of variables/methods defined in an interface in Java 9?
- Can we initialize static variables in a default constructor in Java?
- What happens if an exception is not handled in a java program?
- What does Interface consist of in Java
- Can we declare the variables of a Java interface private and protected?
- What happens if NullPointerException is not handled in main method of java?
- Can we declare an interface with in another interface in java?
- Can we overload methods of an interface in Java?
- What is overloading? What happens if we overload a main method in java?
- Can we cast an object reference to an interface reference in java? If so when?

Advertisements