answersLogoWhite

0


Best Answer

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

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Short answer: no.

If the purpose of creating such a beast is to prevent the class from being instantiated, use a private no-arg constructor.

abstract = means the class cannot be instantiated except through a sub-class.

final = means the class cannot be sub-classed.

Seems the only reason to want to do this is to create a "utility" class - only static methods that other classes will use, maybe as a way of collecting common code in one spot. This is a very common practice, and utility classes should be declared final with private constructors.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes it can be a final class..but you cannot declare instance of it in sub class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why method inner classes can only access final variables from method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


What is similarities between interface and classes?

The similarities are: a. They are both java basic object types b. They both can contain variables and methods (With difference being class methods have implementation code whereas the interface methods can only have declarations) c. They can both be inherited using Inheritance (extends keyword for classes and implements keyword for interfaces)


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.