answersLogoWhite

0

Why use final in java?

Updated: 8/9/2023
User Avatar

Wiki User

12y ago

Best Answer

The use of final denotes that a variable is a constant. For example:

final int PI = 3.14;

makes PI a constant. This is useful because now PI can't be changed, even accidentally, by you, the program, another programmer, or a user. Once a value is final, it is permanent, and can be used as a constant.

Note: usually programmers capitalize the names of constants, just to make it clear that the value is a constant.

User Avatar

Wiki User

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

Wiki User

11y ago

They are unrelated. Static means that there is only one copy of the variable for the entire class - no matter how many objects you have. Final means the value can't change. A variable can be only static, only final, both, or none.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The final keyword can be used in many ways and has many uses in Java.

* A java variable that is declared with the keyword final can be assigned only once. They cannot be modified.

* A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialize it. * Java classes declared as final cannot be extended. It cannot be inherited by any other class.

* Methods declared as final cannot be overridden in the sub classes.

* final parameters - values of the parameters cannot be changed after initialization. * Java local classes can only reference local variables and parameters that are declared as final. * A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Final Variables

Declaring a variable with the final keyword makes it impossible to reassign a different value to that variable once it has been initialized with an explicit value (notice we said explicit rather than default). For primitives, this means that once the variable is assigned a value, the value can't be altered. For example, if you assign 10 to the int variable x, then x is going to stay 10, forever. That's pretty straightforward for primitives, but what does it mean to have a final object reference variable? A reference variable marked final can't ever be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed. In other words, if you have a final employee object variable, you can modify aspects of the employee but you cannot have the variable refer to say a manager.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Protected members can be accessed by classes that inherit from the class with the protected members. This allows direct access to that member for inheriting classes, without granting access to all classes (public).

This is often useful for utility methods that should be shared with inheriting classes, as well as variables that need to be accessed by inheriting classes.

As a brief example, a vehicle class with a private variable for the maximum number of passengers couldn't be extended to accommodate different maximum values (e.g. a car, truck, and airplane would all be forced to carry the same maximum number of passengers).

By making the variable protected, each subclass can specify their capacity without allowing outside classes the ability to specify the vehicle's capacity.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Finalize method is used to remove non-java resources

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Final variables are stored in the same place all other variables are stored. "The JVM Heap"

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why use final in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is unsigned integer constant are available in Java?

yes use the final keyword, normally variables declared final use all capital letters but this is not required final int A = 10;


Do you use virtual functions in Java?

Every method in java that isn't a class (static) method is automatically "virtual." If you want to disable virtual overrides, make the method "final" in the base class.


What are sealed classes in java?

Final classes are sealed classes in java I guess.


What is public static final variable in java?

-Public to all - Static to access with Class Name - Final to change( constant and not alowed to change) Just use it along with class name. (As implied above, a 'public static final' variable in Java is what other languages would call a Constant. )


What modifier do you use to prevent that class from having any subclasses?

In Java, you use the final modifier to prevent a class from having any subclasses.


How does the final keyword affect the behavior of Object type in Java?

In Java, the final keyword specifies that the object created cannot be further redefined or derived.


Can you make a class final in java?

yes of course... using the final keyword


How do you prevent class extending in java?

Declare the class as final. final class A{ ... }


Can you declears static in a local variable in a method in java?

we cannot use the staic keyword inside the method... But we can use the final keyword inside the method....


Can you use pointers in java?

Java does not support Pointers and hence you cannot use it in Java.


Final classes in java?

A class declared as final means that no other class can inherit from it.


What is the return type of finally method in java?

The final and finally keywords have no impact on the return type of a method in Java.