Professional Documents
Culture Documents
Final Keyword
Final
• final keyword is used in different contexts. First of all, final is a non-access modifier
applicable only to a variable, a method or a class.
• When a variable is declared with final keyword, it’s value can’t be modified, essentially, a
constant. We must initialize a final variable, otherwise compiler will throw compile-time
error.
• When a class is declared with final keyword, it is called a final class. A final class cannot be
extended(inherited). Used to create immutable and to prevent inheritance.
• When a method is declared with final keyword, it is called a final method. A final method
cannot be overridden.
Final
Final keyword in java is used to restrict the user. The java final keyword can be used in many context.
The final keyword can be applied with the variables, a final variable that have no value it is called blank
final variable or uninitialized final variable. It can be initialized in the constructor only.
Final Variable Example:
}
class Bike9 }
{
final int speedlimit=90;//final variable
void run()
{
speedlimit=400;
}
public static void main(String args[])
{
Bike9 obj=new Bike9();
obj.run();
Final - method
class Bike
{
final void run() public static void main(String args[])
{ {
System.out.println("running"); Honda honda= new Honda();
} honda.run();
} }
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely with 10
0kmph");
}
Final - Class
}
In stringbuffer
class Gfg
{
public static void main(String[] args)
{
// a final reference variable sb
final StringBuilder sb = new StringBuilder("Geeks");
System.out.println(sb);
System.out.println(sb);
}
}
// final variable will throw compile-time error
class Gfg
{
static final int CAPACITY = 4;
class Gfg
{
public static void main(String args[])
{
// local final variable
final int i;
i = 20;
System.out.println(i);
}
}
// Java program to demonstrate final
// with for-each statement
class Gfg
{
public static void main(String[] args)
{
int arr[] = {1, 2, 3};
// final variable
final int b = 6;
class MM extends QQ {
catch (ArithmeticException e) {
System.out.println("In catch block");
System.out.println("Dividing by zero but caught");
}
finally
{
System.out.println("Executes whether exception occurs or not");
}
}
}
Case 2 : Exception occurs and
corresponding catch block matches
// Java program to illustrate finally in
// Case where exceptions occur
// and match in the program
class C {
public static void main(String[] args)
{
int k = 66;
try {
System.out.println("In try block");
int z = k / 0;
// Carefully see flow dosen't come here
System.out.println("Flow dosen't came here");
}
catch (ArithmeticException e) {
System.out.println("In catch block");
System.out.println("Dividing by zero but caught");
}
finally
{
System.out.println("Executes whether exception occurs or not");
}
}
Case 3 : Exception occurs and
corresponding catch block not
found/match
// Java program to illustrate finally in
// Case where exceptions occur
// and do not match any case in the program
class D {
public static void main(String[] args)
{
int k = 15;
try {
System.out.println("In try block");
int z = k / 0;
}
catch (NullPointerException e) {
System.out.println("In catch block");
System.out.println("Dividing by zero but caught");
}
finally
{
System.out.println("Executes whether exception occurs or not");
}
}
}
finalize method
class Hi {
public static void main(String[] args)
{
Hi j = new Hi();
j = null;
// Driver class
public class AbstractDemo
{
public static void main(String args[])
{
B b = new B();
b.m1();
b.m2();
}
}
1) Like C++, in Java, an instance of an abstract class cannot
be created, we can have references of abstract class type
though.
abstract class Base {
abstract void fun();
}
class Derived extends Base {
void fun() { System.out.println("Derived fun() called"); }
}
class Main {
public static void main(String args[]) {
class Main {
public static void main(String args[]) {
Derived d = new Derived();
d.fun();
}
}
4) Abstract classes can also have final methods (methods
that cannot be overridden). For example, the following
program compiles and runs fine.
// An abstract class with a final method
abstract class Base {
final void fun() { System.out.println("Derived fun() called"); }
}
class Main {
public static void main(String args[]) {
Base b = new Derived();
b.fun();
}
}
static keyword in java
static is a non-access modifier in Java which is applicable for the
following:
blocks
variables
methods
nested classes
To create a static member(block,variable,method,nested class),
precede its declaration with the keyword static. When a member is
declared static, it can be accessed before any objects of its class are
created, and without reference to any object. For example, in below
java program, we are accessing static method m1() without creating
any object of Testclass.
// Java program to demonstrate that a static member
// can be accessed before instantiating a class
class Test
{
// static method
static void m1()
{
System.out.println("from m1");
}
// static block
static {
System.out.println("Static block initialized.");
b = a * 4;
}
// static block
static {
System.out.println("Inside static block");
}
// static method
static int m1() {
System.out.println("from m1");
return 20;
}
}
• Static methods
• When a method is declared with static keyword, it is
known as static method. The most common example of
a static method is main( ) method.As discussed above,
Any static member can be accessed before any objects
of its class are created, and without reference to any
object.Methods declared as static have several
restrictions:
• They can only directly call other static methods.
• They can only directly access static data.
• They cannot refer to this or super in any way.
// java program to demonstrate restriction on static methods
class Test
{
// static variable
static int a = 10;
// instance variable
int b = 20;
// static method
static void m1()
{
a = 20;
System.out.println("from m1");
// instance method
void m2()
{
System.out.println("from m2");
}
class GFG
{
public static void main (String[] args)
{
// creating an Integer Object
// with value 10.
Integer i = new Integer(10);
//Autoboxing of char
Character gfg = 'a';
// Auto-unboxing of Character
char ch = gfg;
System.out.println("Value of ch: " + ch);
System.out.println("Value of gfg: " + gfg);
}
}
/* Java program to illustrate autoboxing */
import java.io.*;
import java.util.*;
class GFG
{
public static void main (String[] args)
{
/* Here we are creating a list
of elements of Integer type.
adding the int primitives type values */
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
list.add(i);
}
}
/* Java program to illustrate autoboxing */
import java.io.*;
import java.util.*;
class GFG
{
public static void main (String[] args)
{
/* Here we are creating a list of elements
of Integer type. Adding the int primitives
type values by converting them into Integer
wrapper Object*/
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
list.add(Integer.valueOf(i));
}
}
// Java program to illustrate find sum
// of odd numbers using autobxing and unboxing
import java.io.*;
import java.util.*;
class GFG
{
public static int sumOfOddNumber(List<Integer> list)
{
int sum = 0;
for (Integer i : list)
{
// unboxing of i automatically
if(i % 2 != 0)
sum += i;
/* unboxing of i is done automatically
using intvalue implicitly
if(i.intValue() % 2 != 0)
sum += i.intValue();*/
}
return sum;
}