wellnwill Marathahalli Bangalore

final keyword


Final keyword is a modifier and can be used for variables, methods and classes.

Final variable

A variable whose value cannot be changed during the program execution is known as final variable. In java, constant is known as final variable.

Ex- final byte b=10; final int a=10;

Non-static final variable

  • An instance variable with the final keyword is called instance final variable or non-static final variable.
  • The default value will not be provided by JVM to instance final variable. We have to perform initialization explicitly otherwise we will get compile time error.
  • We have to provide value for instance final variable before complete execution of constructor. We can initialize instance final variable in following places: 1. At a time of declaration 2. Inside non-static block 3. Inside constructor.

class Test196
{
	
 final int a;
 
 public static void main(String args[])
 {
  System.out.println("a is instance final variable");
 }
}
Output-CTE- variable a not initialized in default constructor

class Test197
 {
	 
 final int a=20; //at a time of declaration
 
 public static void main(String argss[])
 {
  System.out.println("a is instance final variable");
 }
}
Output- a is instance final variable.



class Test198
{
final int a;
 {
 a=20; // inside non static block
 }
 public static void main(String argss[])
 {
  System.out.println("a is instance final variable");
 }
}

Output- a is instance final variable.

class Test0199
{ 
final int a;
a=20;

 Test0199()
  {
  }
 public static void main(String argss[])
 {
  System.out.println("a is instance final variable");
}
}
Output-CTE

class Test200
{
  public static void main(String argss[])
  {
     final int a;
     a=20;
  System.out.println("a is instance final variable");
  }
} 
Output- a is instance final variable.



class Test201
{ 
final int a;
 {
 a=20;
 }
public static void main(String argss[])
 {
  System.out.println("a is instance final variable");
 }
}
Output- a is instance final variable.


class Test202
{
final int a;

Test202()
  {
   a=20;
  }
  
 public static void main(String argss[])
 {
  System.out.println("a is instance final variable");
 }
}
Output- a is instance final variable.

class Test203
{
public static void main(String argss[])
 { 
    final int a=10;
    a=30;
  System.out.println("a is instance final variable");
 }
}
Output-CTE-cannot assign value to a final variable a.







static final variable

• Static variable with the final keyword is called static final variable. • The default value will not be provided by JVM to the static final variable. We have to perform initialization explicitly otherwise we will get compile time error. • We have to provide value to the static final variable before completion of class loading. We can initialize the static final variable in following places: 1. At the time of declaration 2. Inside static block

class Test204
{
 static final int a; 
 public static void main(String argss[])
 {
  System.out.println("a is static final variable");
 }
}
Output- CTE- variable a not initialized in default constructor.

class Test205
{
 static final int a=20; 
 public static void main(String argss[])
 {
  System.out.println("a is static final variable");
 }
} 

Output- a is static final variable.


class Test206
{ 
 static final int a;
 static
 {
 a=20;
 }
public static void main(String argss[])
 {
  System.out.println("a is static final variable");
 }
}
Output- a is static final variable.


Local final variable

• A variable declares inside a method, block, a constructor is known as local variable or stack variable. For temporary requirement in the program, we use a local variable. • A local variable with final keyword is called local final variable. • The default value will not be provided by JVM to a local final variable. • We have to provide value to local final variable explicitly if and only if we are accessing the value.

class Test207
{
 public static void main(String argss[])
 {
  final int a; 
  System.out.println("a is local final variable");
 }
}

Output- a is local final variable

class Test208
{
 public static void main(String argss[])
 {
  final int a; 
  System.out.println(a);
 }
}

Output-CTE-variable a might not have been initialized


  • The applicable modifier for local variable is final only. By mistake if we are trying to apply any other modifier then we will get Compile time error.
  • We can apply final modifier only for local variable. No other modifiers are applicable for local variable.
class Test209
{
 public static void main(String argss[])
 { 
  static int a=20;
  public int b=30;
  private int c=40;
  System.out.println(a);
 }
}O/P-CTE

Final class

We cannot create sub class for final class.


final class A
{
  void m1()
 {
 System.out.println("m1()in A");
 }  
}  
class B extends A
{  
}  

class Test210
{ 
 public static void main(String args[])
 { 
 System.out.println("cannot create subclass for final class");     
 }
}  
Output-CTE- cannot inherit from final A.


Final method

We cannot override the final method in subclass but the final method will be available in the subclass.

class Parent
{
	
final void lifeStyle()
 {
 System.out.println("eat,sleep,work");
 }  
} 
 class Child extends Parent
{ 
  void lifeStyle()
 {
 System.out.println("party,eat,sleep,play,study");
 } 
}  
class Test0210
{ 
 public static void main(String args[])
 { 
 Child c1=new Child();
 c1.lifeStyle(); 
  }
}  

Footer with Map

Top