Results 1 to 9 of 9
Like Tree4Likes
  • 1 Post By jim829
  • 1 Post By SurfMan
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Does 'final' make a variable a constant, or a combination of 'final' and 'static'?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Does 'final' make a variable a constant, or a combination of 'final' and 'static'?

    The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.
    - Source

    It is the final modifier which guarantees that the value won't change, and that is what a constant is, a field with an unchanging/constant value. So why are they saying that it is a combination of static and final modifiers that make a constant?

    Besides, I tried the following test, and it runs successfully. But then why are they saying that.

    Java Code:
    class CanThereBeInstanceConstants {
    	private final int INTEGER = 1;
    
    	public static void main(String[] args) {
    		System.out.println(new CanThereBeInstanceConstants().INTEGER); 
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    Typically, constants are defined values shared across all instances of a given class. So they are declared final and static.
    A final instance field (i.e non-static) may be assigned different values in different instances of the same class.

    Java Code:
    public class FinalExample {
       final long time = System.nanoTime();
       public static void main(String[] args) {
           FinalExample f1 = new FinalExample();
           FinalExample f2 = new FinalExample();
           System.out.println(f1.time);
           System.out.println(f2.time);
       }
    }
    Also, a local final value in a method may be assigned a new value every time the method
    is entered. But it can only be assigned once per method invocation.

    Regards,
    Jim
    Last edited by jim829; 02-25-2016 at 03:56 AM.
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    I find the explanation The final modifier indicates that the value of this field cannot change always a bit tricky (technically it's correct), but I'd like to think of it more as: the variable cannot be reassigned. Why? If the final variable is an Object, it's perfectly valid to change the Object.

    This wil produce an error...
    Java Code:
    final int meaningOfLife = 42;
    ...
    public void something() {
       meaningOfLife = 43;
    }
    .. and this too...
    Java Code:
    final Person boss = new Person("Hank");
    ...
    
    public void something() {
       boss = new Person("Peet");
    }
    ... but this is perfectly valid...
    Java Code:
    final Person boss = new Person("Hank");
    ...
    
    public void something() {
       boss.setName("Peet");
    }
    Zarah likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    Quote Originally Posted by SurfMan View Post
    I find the explanation The final modifier indicates that the value of this field cannot change always a bit tricky (technically it's correct), but I'd like to think of it more as: the variable cannot be reassigned. Why? If the final variable is an Object, it's perfectly valid to change the Object.
    The problem with avoiding the "technically correct" answer is that it then often leads to the "Java is pass by reference" as it loses the key fact that the value of a reference variable is the reference itself, and not any value inside the object.
    Zarah likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    Quote Originally Posted by Tolls View Post
    The problem with avoiding the "technically correct" answer is that it then often leads to the "Java is pass by reference" as it loses the key fact that the value of a reference variable is the reference itself, and not any value inside the object.
    Not sure if you agree with me here... Maybe this would be more accurate: "The final modifier indicates that the value of this field, which for objects is the reference to the object itself, cannot change. In other words, you can not assign a different reference to this field. The final keyword does not imply anything about the (im)mutability of the object itself".

    Well, you get the point...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    Oh yes.
    I just don't think avoiding the fact that the value of a reference variable is (essentially) a pointer, and nothing to do with any data in the thing its pointing to, and that 'final' means that value is unchangeable runs the risk of oversimplifying to the point where it causes issues further down the line. Like with the whole 'pass by reference' argument that turns up every so often.

    I think people learning Java should know that the variable has a value, and final means that value is unchangeable, and that that value has nothing to do with any data in the object that variable might be pointing at.
    Zarah likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    mbschultz97 is offline Member
    Join Date
    May 2014
    Location
    Texas
    Posts
    73
    Rep Power
    0

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    Is it a bad sign that this is in the "New to Java" section and I barely understand what you guys are saying?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    No. It just means you have not encountered the final keyword. I find most topics fit the new to Java category. And some folks (wrongly imho) put advanced computer science topics in advanced java. But their solutions do not require advanced java concepts.

    I guess some things like threads, class loaders, JNI, and certain generic constructs might be considered advanced Java. But it is a rather subjective term.

    Regards,
    Jim
    Last edited by jim829; 02-26-2016 at 05:12 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Does 'final' make a variable a constant, or a combination of 'final' and 'static'

    Thank you everyone for this discussion. It learned a couple of important things.

Similar Threads

  1. Replies: 6
    Last Post: 06-30-2015, 07:19 PM
  2. static final int DONE = (1 << 9) - 1; ???
    By alacn in forum New To Java
    Replies: 2
    Last Post: 07-26-2010, 06:31 PM
  3. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 04:01 PM
  4. static final field
    By techie.it19 in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •