Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:38:49+00:00 2026-05-31T07:38:49+00:00

We have a method whose parameter is defined as final,and then change the value

  • 0

We have a method whose parameter is defined as final,and then change the value of this parameter(in this method) and return it, it should not be changed as we passed it as final.
But this is not the case.

Please help me to understand this!

public static void main(String[] args) {
        EmployeeBean e1 = new EmployeeBean("1", "jitu");
System.out.println("object before set >>" + e1.getEmpName());

EmployeeBean newObj = x.changeFinalvalue(e1);
        System.out.println("object after set >>" + newObj.getEmpName());


public EmployeeBean changeFinalvalue(final EmployeeBean x) {
        x.setEmpName("Jeet");
        return x;
    }

Output :
object after set >>jitu

object after set >>Jeet //doubt:it has to be ‘jitu’ only

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:38:51+00:00Added an answer on May 31, 2026 at 7:38 am

    You are not changing which object is being referred to by the parameter (which is not allowed here). Instead you are changing the state of the object (which is allowed here).

    In other words, when you pass your e1 EmployeeBean object into the method, the x parameter refers to the same object that e1 refers to, and within the method, this cannot change, and so you can’t do this:

    public EmployeeBean changeFinalvalue(final EmployeeBean x) {
      x = new EmployeeBean(); // changing the reference is not allowed!
      return x;
    }
    

    But what you’re doing — changing the state of the object — is allowed.

    The only way that I know to prevent problems like this is to make a deep copy of the object passed in as parameter and work on the copy/clone object. You could also make the EmployeeBean immutable, but then it wouldn’t be much of a Bean, now would it?

    Edit: I figured that this question has had to have been asked here many times, and on a little searching, I’ve found that of course it has. Please look at these links for a good discussion on this. Note that this is a general issue about final variables whether they are method parameters or class fields:
    java-final-keyword-for-variables
    java-final-modifier

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method where I would like to return an object instance of
I have this method that changes an larger image source on click. I need
Let's say I've got a method that has a parameter, whose valid values are
I'm having an issue with my grails application. I have a form whose method
I have a method that has 2 output parameters. The method should take an
Okay, I'm losing my mind over this one. I have a method in my
Suppose I have a file X.h which defines a class X, whose methods are
I have method in a class that I need to make sure is only
I've got some 3rd party beans that have method signatures that fit quite well
I create small application which is media player. I have method where I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.