final variables in Java Last Updated : 23 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, we can use final keyword with variables, methods, and classes. When the final keyword is used with a variable of primitive data types such as int, float, etc), the value of the variable cannot be changed. Example 1: Usage of final with primitive datatype Java // Java Program to illustrate Use of Final Keyword // With Primitive Datatypes // Main class class GFG { // Main driver method public static void main(String args[]) { // Final primitive variable final int i = 10; i = 30; // Error will be generated above } } Output: Now you must be wondering what if we do use final keyword non-primitive variables, let us explore the same as we did above with the help of an example. Note: Non-primitive variables are always references to objects in Java, the members of the referred object can be changed. final for non-primitive variables just means that they cannot be changed to refer to any other object. Example 2: Usage of final with primitive datatype Java // Java Program to illustrate Use of Final Keyword // With Primitive Datatypes // Class 1 class Helper { int i = 10; } // Class 2 // main class class GFG { // Main driver method public static void main(String args[]) { final Helper t1 = new Helper(); t1.i = 30; // Works // Print statement for successful execution of // Program System.out.print("Successfully executed"); } } OutputSuccessfully executedNote: A final variable can't be declared inside a function, this is so because scope of local variables of a function are limited to scope of function and declaring it their will be against the principle of immutability. If program demands a constant value then the final variable should be declared at class level and may be used in function. Comment More infoAdvertise with us Next Article Private and Final Methods in Java K kartik Follow Improve Article Tags : Java Java-keyword Practice Tags : Java Similar Reads Final Local Variables in Java In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u 3 min read final Keyword in Java The final Keyword in Java is used as a non-access modifier applicable to a variable, a method, or a class. It is used to restrict a user in Java. We cannot use the final keyword in a block. It restricts the user from accessing that particular part of the program, such as restricting the modification 11 min read Private and Final Methods in Java Methods in Java play an important role in making the code more readable, support code reusability, and defining the behaviour of the objects. And we can also restrict the methods based on requirements using the keywords and modifiers such as final and private. These two have different, distinct purp 5 min read Effectively Final Variable in Java with Examples A final variable is a variable that is declared with a keyword known as 'final'. Example: final int number; number = 77; The Effectively Final variable is a local variable that follows the following properties are listed below as follows: Not defined as finalAssigned to ONLY once. Any local variable 4 min read Rules For Variable Declaration in Java Variable in Java is a data container that saves the data values during Java program execution. Every variable is assigned a data type that designates the type and quantity of value it can hold. Variable is a memory location name of the data. A variable is a name given to a memory location. For More 2 min read Unnamed Patterns and Variables in Java Java programming language offers multiple features that allow developers to write rich and concise code. Among these features, there are unnamed patterns and variables which enables developers to work with data in a more flexible and elegant way. In this article, we will study about unnamed patterns 4 min read Like