Results 1 to 7 of 7
  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default assigning final instance variable inside a constructor vs method

    Hi all,


    How is it that one can assign a final instance variable inside a constructor but not inside a method?

    Thank you.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: assigning final instance variable inside a constructor vs method

    Hi Amanda,

    I ran into a similar question some time ago, and I found my answer here!

    Final variable-constructor method.

    This might help you out, otherwise just wait and someone will probably give you a good answer! =)

    Cheers,
    Z!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: assigning final instance variable inside a constructor vs method

    Because a final attribute has to have a value by the time its constructor has finished, and that value cannot be changed.
    By having a method that can change that value implies that that method could be called elsewhere than the constructor, so the compiler flags that as an error.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: assigning final instance variable inside a constructor vs method

    Why don't people experiment anymore? The compiler won't bite nor are you turned into a newt by experimenting; try the following code snippet and see what the compiler has to say about it; initialize the final variables outside of the methods or never initialize them at all:

    Java Code:
    public class Test {
    
    	private static final int s;
    	private final int i;
    	
    	private static void staticMethod() {
    		s= 42;
    		i= 54;
    	}
    	
    	private Test() {
    		s= 42;
    		i= 54;
    	}
    
    	private void method() {
    		s= 42;
    		i= 54;
    	}
    
    	public static void main(String[] args) {
    
    		staticMethod();
    		new Test().method();
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: assigning final instance variable inside a constructor vs method

    the above code throws torrents of errors like "cannot assign a value to final variable s"

    " non static variable i cannot be referenced from a static context"

    "cannot assign a value to final variable s"

    and so on!!1

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: assigning final instance variable inside a constructor vs method

    Quote Originally Posted by JosAH View Post
    The compiler won't bite nor are you turned into a newt by experimenting;
    Damn you!
    I'm currently typing this with my little newty fingers, thanks to your "Why don't people experiment anymore" bit of irresponsibility...
    <whatever noise a newt makes>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: assigning final instance variable inside a constructor vs method

    Quote Originally Posted by Tolls View Post
    Damn you!
    I'm currently typing this with my little newty fingers, thanks to your "Why don't people experiment anymore" bit of irresponsibility...
    <whatever noise a newt makes>
    Oh, sorry, I forgot to mention that it does wiggle its genitals towards you general direction now and then; maybe that turned you into a newt ...

    kind regards,

    Jos

    ps. you'll get better ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  3. Replies: 7
    Last Post: 07-10-2011, 08:59 PM
  4. Final variable-constructor method.
    By D4rkNrG in forum New To Java
    Replies: 9
    Last Post: 10-13-2010, 11:18 AM
  5. Replies: 20
    Last Post: 06-03-2009, 05:33 PM

Posting Permissions

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