
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the final modifier in Java?
The final modifier can be associated with methods, classes and variables. Once declared final −
- A final class cannot be instantiated.
- A final method cannot be overridden.
- A final variable cannot be reassigned.
Example
class TestExample { final int value = 10; public static final int BOXWIDTH = 6; static final String TITLE = "Manager"; public final void changeName() { System.out.println("This is a final method"); } } final class Demo{ } public class FinalExample extends TestExample { public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.value); System.out.println(obj.BOXWIDTH); System.out.println(obj.TITLE); obj.changeName(); } }
Output
10 6 Manager This is a final method
- Related Questions & Answers
- How to use a final or effectively final variable in lambda expression in Java?
- private access modifier in Java
- default access modifier in Java
- protected access modifier in Java
- public access modifier in Java
- What is the use of WITH ROLLUP modifier in MySQL?
- What does the modifier transient in Java do?
- What does the modifier volatile in Java do?
- Demonstrate Private access modifier in Java
- What does synchronized modifier do in Java?
- What does abstract modifier in Java do?
- Why Protected access modifier used in Java?
- final keyword in Java
- Final variable in Java
- Final class in Java
Advertisements