beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Final method parameters in java

By Chaitanya Singh | Filed Under: OOPs Concept

In the last tutorial we discussed about final keyword. In this post we are gonna discuss about final method parameters. You must have seen the use of final keyword in method arguments. Lets take an example to understand it:

class FinalDemo
{
   public void myMethod(int num, final String str){
	// This is allowed as num is not final
	num = num+10;

	/* This is not allowed as String str is final and 
	 * we cannot change the value of final parameter.
	 * we can just use it without modifying its value.
	 */
	str = str+"XYZ"; 
		
	System.out.println(num+str);
  }
  public static void main(String args[]){
	FinalDemo obj= new FinalDemo();
	obj.myMethod(10, "BeginnersBook.com");
  }
}

Output: The above program would throw the following compile time error because we are trying to change the value of final parameter.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The final local variable str cannot be assigned.

If we comment out the statement str = str+"XYZ"; in above program then it would run fine without any issues.

Enjoyed this post? Try these related posts

  1. Does Java support Multiple inheritance?
  2. Abstract Class in Java with example
  3. hybrid inheritance in java with example program
  4. Java – Default constructor with example
  5. Garbage Collection in Java
  6. Difference Between Abstract Class and Interface in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Index
  • Java Introduction
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap