What does the final modifier mean on a method parameter?
Use the final modifier on formal parameters to methods when you want to make it clear that the supplied value can be not modified inside the method. The compiler would complain as soon as you accidentally try to assign it a new value inside the method. Anonymous classes that need to access method's parameters are another use case of final method parameters.
- The final keyword on instance and local variables of primitives type only makes primitives behave like constants. You can not reassign value to them.
- The final keyword on a reference variable only restricts the reference itself can not be changed but you still can change the context of the object that the reference points to.
- The final keyword on a formal parameter restricts the formal parameter can not be reassigned inside the method. The final keyword is not part of the method's signature, it can be removed from its overriding method in its subclass.
Most Recent java Faqs
- How to avoid an java.util.ConcurrentModificationException with ArrayList?
- How to convert a given array to a list in Java?
- How to make Java objects eligible for garbage collection?
- What are local variables in Java?
- What are instance variables in Java?
- How many backslashes?
- What are class variables in Java?
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?(24745)
- What is runtime polymorphism in Java?(18326)
- How to add BASIC Authentication into HttpURLConnection?(16083)
- What is String literal pool?(14754)
- Can the run() method be called directly to start a thread?(13991)
- What does Class.forname method do?(10593)
- Can transient variables be declared as 'final' or 'static'?(10445)