In java, static blank final variable is a variable which is declared with final keyword but not initialised at declaration time. It can only be initialised in static block.
Example
class StaticBlankFinalTest{ //static blank final variable static final String website; static{ website = "w3schools.com"; System.out.println("website = " + website); } } public class Main { public static void main(String args[]){ //creating object of StaticBlankFinalTest Class StaticBlankFinalTest obj = new StaticBlankFinalTest(); } } |
Output
website = w3schools.com |
We will get compile time error, if we try to initialize it through any other way except static block.
Example
class StaticBlankFinalTest{ //static blank final variable static final String website; StaticBlankFinalTest(String website){ System.out.println("website = " + website); } } public class Main { public static void main(String args[]){ //creating object of StaticBlankFinalTest Class StaticBlankFinalTest obj = new StaticBlankFinalTest("w3schools.com"); } } |
Output
Main.java:7: error: variable website might not have been initialized } ^ 1 error |
Java interview questions on final keyword
- what is final in java?
- What is final variable in java?
- What is final method in java?
- What is final class in java?
- What is blank final variable in java?
- What is static blank final variable in java?
- What is final parameter in java?
- Can we initialize blank final variable in java?
- Can we declare the main method as final?
- What is the use of final keyword in java?
- Can we change the state of an object to which a final reference variable is pointing?
- Difference between abstract method and final method in java?
- Can we change the value of an interface field?
- Can we declare constructor as final in java?