
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Unreachable statement using final variable in Java
Unreachable statements are those that don’t get executed when the code is being executed. This could be so because −
- There is a return statement before the code.
- There is an infinite loop in the code.
- The execution of the code is terminated forcibly before it executes.
Here, we will see how the unreachable statement can be used with the ‘final’ keyword −
Example
class Demo_example{ final int a = 56, b = 99; void func_sample(){ while (a < b){ System.out.println("The first value is less than the second."); } System.out.println("This is an unreachable statement"); } } public class Demo{ public static void main(String args[]){ Demo_example my_instance = new Demo_example(); my_instance.func_sample(); } }
Output
/Demo.java:11: error: unreachable statement System.out.println("This is an unreachable statement"); ^ 1 error
A class named ‘Demo_example’ contains two final integers (basically like constants), and a function named ‘func_sample’, that compares the two integers. Relevant message is displayed on the console. Another class named ‘Demo’ is defined and it contains the main function. In this function, an instance of the Demo class is created, and the function ‘func_sample’ is called on this instance. Relevant output is displayed on the console.
- Related Articles
- Unreachable statement using the non-final variable in Java
- Final variable in Java
- final local variable in Java
- Instance variable as final in Java
- What is a final variable in java?
- What is static blank final variable in Java?
- Can we initialize blank final variable in Java
- Unreachable Code Error in Java
- What is blank final variable? What are Static blank final variables in Java?
- How to use a final or effectively final variable in lambda expression in Java?
- What is a blank uninitialized final variable in java?
- What is blank or uninitialized final variable in Java?
- What are unreachable blocks in java?
- What are unreachable catch blocks in Java?
- How many ways are there to initialize a final variable in java?

Advertisements