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 611617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:48:13+00:00 2026-05-13T17:48:13+00:00

I’ve just come across some code that’s confusing me slightly; there are really 2

  • 0

I’ve just come across some code that’s confusing me slightly; there are really 2 variations that I’d like to clarify.

Example 1:

public String getFilepath(){
        final File file = new File(this.folder, this.filename);
        return file.getAbsolutePath();
    }

What would be the purpose of declaring file “final“? Since Java primitives are passed by value, and getAbsolutePath() is just returning a String, the variable won’t be final on the other side (calling method), will it? And since the file variable only exists within the scope of these 2 lines, I can’t really see any purpose of the final keyword. Is there something I’m missing? Anyone see a reason to do this?

Example 2:

public String getFilepath(){
        final File file = new File(this.folder, this.filename);
        return file;
    }

Since here the actual object is being returned… Does that mean the file variable will be constant/final on the other side…? It doesn’t seem to make sense.

In general, it seems to me that you pass a variable, without it’s access type. As in, I can have a private variable in a function with a public get function that returns it – but the variable that receives it by calling the function has to specify an access modifier. So if it specifies public, the returned variable will be public in that scope. If it specifies private, the returned variable will be private in that scope. Is there a difference with final? Is the “constancy” of a variable something that can be passed? This strikes me as rather improbable, considering what I know of Java.

Or am I missing the point entirely and there’s some other purpose of the final keyword in the above code?

Edit:

I checked back with the original developer who wrote the code, and he said he only put the final keyword in because he had originally thought the method would be a lot longer and wanted to ensure that the file stayed constant throughout. He also said that he generally declares variables that should not be changed as final, as a rule across the board and sort of on principle – a point that both the answers below mentioned. So it seems I was reading too much into a simple extra keyword included for standards reasons. Thanks everyone!

  • 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-13T17:48:13+00:00Added an answer on May 13, 2026 at 5:48 pm

    Some people might tell you that there’s a performance benefit to using final, but that is, in no way, conclusively proven.

    The primary benefit of the final keyword is for the programmer to indicate that a class, method, or field should not be changed.

    Bear in mind that declaring a variable final does not make the referenced object immutable. It just means that the variable cannot have its value reassigned. You can still run methods of the variable file that could change the File object internally.

    In the two methods you give, I see no value in making the file variable final. Some code conventions advocate making all variable final unless the need to be modified. Some people don’t subscribe to that. I consider it a variation on the precautionary principle.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer package org.com; import java.lang.*; import java.util.*; public class Calculator {… May 14, 2026 at 6:33 pm
  • Editorial Team
    Editorial Team added an answer To illustrate 'cross-domain', the following table gives the overview of… May 14, 2026 at 6:33 pm
  • Editorial Team
    Editorial Team added an answer The nullable integer int? or Nullable<int> is a value type… May 14, 2026 at 6:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.