Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: In Java, are only local variables actually final

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    India
    Posts
    20
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default In Java, are only local variables actually final

    I'd want to use Java 9's try-with-resources capability by putting a reference variable in try with resources rather than the variable's full declaration. I also realise that in order to do so, I must adhere to the following rule: the variables used as try-with-resources resources must be final or nearly so. I'll begin with a local variable and then progress to an instance variable.
    Variable at the local level:
    -I create a final variable that follows the specified rule and builds correctly:

    Java:
    public static void main (String[] args) throws IOException{
            final FileWriter fw = new FileWriter ("test.txt");
            try(fw) {
                //some code
            }
        }

    -If I additionally removed the final keyword, it would be recompiled because fw is basically treated as a final variable that is only initialised once and never transmitted.

     Java:
    public static void main (String[] args) throws IOException{
         FileWriter fw = new FileWriter ("test.txt");
        try(fw) {
            //some code
        }
    }

    Variable example:
    Will this paradigm, however, work for the instance variable? Let's give it a go.

    - Let's start with the last instance variable, which follows the rule and builds correctly:

     public class Test {
      final FileWriter fw = new FileWriter ("a.txt");
     
        void m1() throws IOException {
            try(fw ) {
                //some code
            }
        }
    }

    -Should it compile again if I remove the final keyword? This should be the final workforce because I am not modifying fw anywhere but simply initialising it once. Unfortunately, this is not going to work:

    public class Test {
       FileWriter fileWriter = new FileWriter ("a.txt");
     
        void m1() throws IOException {
            try(fileWriter) {
                //some code
            }
        }
    }

    This sends me a message: the variable used as a test resource with resources must be final or genuinely final. So, after all of that, I return to my original question. Is it possible for an instance variable to be effectively final, or is this phrase reserved for local variables? As I've just demonstrated, I never alter a variable following this post (which should be deemed essentially final), yet the compiler never views it as such.

  2. #2
    Junior Member
    Join Date
    Mar 2023
    Location
    Vancouver, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: In Java, are only local variables actually final

    If you don't need to access the instance of FileWriter outside of the try block, you could do this:
    public class Test {
        void m1() throws IOException {
            try(var fileWriter = new FileWriter ("a.txt")) {
                //some code using fileWriter ...
            }
        }
    }

  3. #3
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    117
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: In Java, are only local variables actually final

    Thank you. My first post. New here. Does local variables refer only to one location? Example: My home network doesn't use a java platform. But some others who want to view my pictures on my home network. Does the local variable get over ridden? Like file name, or file destination. Assuming this being a network issue. I have an iPhone and using wi-Fi connection and crome book on the network.

Similar Threads

  1. Replies: 2
    Last Post: December 14th, 2012, 05:31 AM
  2. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM