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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:30:27+00:00 2026-05-25T10:30:27+00:00

I am writing a program that has two final variables that I wish to

  • 0

I am writing a program that has two final variables that I wish to use, I need these to be set at the time that I actually run the class because they are likely to be different each instance.

I have the initialization the same as any other class variable I wish to use where I initialize a name and type but not a value.

   public final String filename, filepath;

In the Constructor I set the values as follows

 public myClass(String value) {
     this.filename = value;
     this.filepath = anotherPartOfValue;
  }

When I do this, I get a warning that “The final field [x] may have already been assigned”

Is there a way to avoid this warning and still keep the final state and set the value in the constructor?

I am using eclipse btw.


Edit:

This is the exact code that gives me the error

import java.io.*;

public class Dirt {

private String[] tables;
private int numTables;
private final String filename, filepath;

public Dirt(String file) {
    this.tables = new String[0];
    this.numTables = 0;

    for (int i = file.length(); i < 0; i--) {
        if (file.charAt(i) == '/') {
            this.filename = file.substring(i);
            this.filepath = file.substring(1, i-1);
        }
    }
}

}

  • 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-25T10:30:27+00:00Added an answer on May 25, 2026 at 10:30 am

    The problem is that you are assigning to the final variables in a loop. There’s nothing to prevent the loop from looping more than once and the if condition being satisfied more than once. (What happens if there are two ‘/’ characters in file? Or none?)

    One way around this is to use temporary String variables in the constructor and then assign them to filename and filepath at the end:

    public Dirt(String file) {
        this.tables = new String[0];
        this.numTables = 0;
        String name = null;
        String path = null;
    
        for (int i = file.length(); i < 0; i--) {
            if (file.charAt(i) == '/') {
                name = file.substring(i);
                path = file.substring(1, i-1);
                // need a break here?
            }
        }
        this.filename = name;
        this.filepath = path;
    }
    

    It’s ugly, but it’s a straightforward way to know for sure that filename and filepath will definitely be assigned and definitely be assigned only once.

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

Sidebar

Related Questions

I'm writing a program that has two libraries that I need to use: v8,
I'm writing a program that has two main functions. Each of the functions will
Unfortunately I don't know much networks. I am writing a program that has two
I am writing a bash program that has IPC between two proccesses (A and
I'm writing a simple little program in VB.NET that has to play two sounds
I'm writing a program that has an NSView embedded in an NSScrollView which user
I'm writing a program that contains a generational garbage collector. There are just two
I'm writing an application which has two classes that provide basically the same functionality
I'm writing a c# program that will launch many child processes. At some time
I'm writing a program that uses a bunch of two-way bindings and the amount

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.