answersLogoWhite

0

I think the main reason is because the inner class may "outlive" the method in which it was created. If this happens, then the memory storing those variables would be lost and we would run into problems when trying to access them. If the variables are defined as final, then Java knows that those values will never change and it can move/copy them when the class is created.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

How do you access final methods from Outer Classes?

The final modifier has nothing to do with access. If your "outer" classes can see the method, they can access them just like any other methods.


Discuss final method and final classes?

a method declared final can not be overridden, and a class declared as final can not be extended by its sub class.


What are the usages of keyword final in java?

It's a built-in keyword that defines an entity that cannot be later modified. It can be used in different aspects within your code (like setting a 'final' class, method (function), or variable).


When do you declare a variable method and a class final?

When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.


What is a specifier?

a specifier tells the JVM how to treat a particular class,method and variable while executing the program. For example, final classes cannot be extended and final methods cannot be overriden and final variables cannot be changed once declared. Likewise,static methods and variables can be accessed without having to instantiate an object for their class


What access attribute should static final variables have?

It should be public


What is difference in use between interfaces and abstract classes in java?

While neither abstract classes nor interfaces can be instantiated in Java, you can implement methods in abstract classes. Interfaces can only define methods; no code beyond a method header is allowed.


Why do you have only public static final variables in interfaces?

Because of the below reasons:Class implementations can change value of fields if they are not defined as final. Then they would become a part of the implementation. An interface is a pure specification without any implementation, hence they are final to prevent accidental modificationsIf they are static, then they belong to the interface, and not the object, nor the run-time type of the object.An interface provide a way for the client to interact with the object. If variables were not public, the implementing classes would not have access to them.Hence variables in interfaces are public static and final


Why can not define private and protected modifiers for variables in interface?

No. Interface variables are supposed to be public static final. Interfaces, like abstract classes, cannot be instantiated, so all variables in an interface must be static final ones. They are public because usually interfaces are used throughout an application, and this will ensure versatility.


Difference between super and final keyword?

this is a reference to the current classsuper is a reference to the super class of the current class (the class from which this class extends). You can use super.super if you want to access the second level class. (The class your parent class extends) The purpose of having thisand super keywords is to differentiate between methods and variables in classes that may have the same name as that in the parent class. Under such situations if we want to ensure that only the methods from a particular class only gets called we can use this and super.


Explain with an example program the uses of final keywords?

The Final keyword is used to ensure that the methods/variables are not modified/overridden in their child classes. ex: public class A { public void final getName(){ ... } } public class B extends A{ public void getName(){ ... } } While trying to compile class B, there would be compilation errors. Since the method getName in the parent class is final, it cannot be overridden in the child class.


When do you declare a method final in java?

You declare a method final in Java when you do not want any subclasses of your class to be able to override the method. I have also heard that this allows the Java compiler to make more intelligent decisions. For example, it supposedly allows Java to decide when to make a method inline. (Note that this is all unconfirmed)