Results 1 to 10 of 10
  1. #1
    D4rkNrG's Avatar
    D4rkNrG is offline Member
    Join Date
    Oct 2010
    Location
    Athens , Greece
    Posts
    5
    Rep Power
    0

    Default Final variable-constructor method.

    Hi i would like to know why cant i initialize a final instance variable from the default constructor the compiler provides?i dont want to assign an explicit value to it.heres an example code:

    class FinalVar{
    final int x;
    // variable x might have not been initialized why???
    public static void main(){
    //....
    }
    }

    thx for your help :)

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    Hello D4rkNrG, welcome to the forums.

    Any variable declared final have to have their values defined. This is not something which can be handled by the constructor as the value is fixed so cannot change.

    Regards.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Ronin View Post
    Hello D4rkNrG, welcome to the forums.

    Any variable declared final have to have their values defined. This is not something which can be handled by the constructor as the value is fixed so cannot change.

    Regards.

    I respectfully disagree. A final class field can be assigned in a constructor but it must be explicitly assigned. e.g.,


    Java Code:
    class FinalVar {
        final int x;
        
        FinalVar() {
            x = 3;
        }
    
        private int getX() {
            return x;
        }
    
        public static void main(String[] args) {
            FinalVar finalvar = new FinalVar();
            
            System.out.println(finalvar.getX());
        }
        
    }


    Original Poster: how would the JVM know what to assign the variable to in an implicit constructor?

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    I shall have to admit, I never realised that could be done that way. Oh the simple things! :D

  5. #5
    D4rkNrG's Avatar
    D4rkNrG is offline Member
    Join Date
    Oct 2010
    Location
    Athens , Greece
    Posts
    5
    Rep Power
    0

    Default

    Doesn't that come in contrast with the definition of a final variable?A final variable can have a value assigned to it and then its value cannot be changed. In this example why cant the default constructor, the JVM provides assign the default value to it?

  6. #6
    D4rkNrG's Avatar
    D4rkNrG is offline Member
    Join Date
    Oct 2010
    Location
    Athens , Greece
    Posts
    5
    Rep Power
    0

    Default

    Oh yeah and one more thing does anyone know how to reply to posts? like the moderator Fubarable did to the member Ronin?Originally posted by Ronin....etc
    Sorry for being a little newbie and thanks in advance! :D

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by D4rkNrG View Post
    Doesn't that come in contrast with the definition of a final variable?A final variable can have a value assigned to it and then its value cannot be changed. In this example why cant the default constructor, the JVM provides assign the default value to it?
    Just think about it, if you do this:
    Java Code:
    int a;
    the compiler assigns some default value to the variable. Final variables get a value assigned one, and than that value can change. If the compiler implicitly initializes a final variable, you'd be stuck with the default value. About the reply thingamajig, if you look at a post, you'll notice a few buttons there, the one that interests you is the Quote button. Also, another one of those buttons is very usefull, namely the Edit button, so you can edit your post instead of double posting.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  8. #8
    D4rkNrG's Avatar
    D4rkNrG is offline Member
    Join Date
    Oct 2010
    Location
    Athens , Greece
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    Final variables get a value assigned one, and than that value can change. If the compiler implicitly initializes a final variable, you'd be stuck with the default value. About the reply thingamajig, if you look at a post, you'll notice a few buttons there, the one that interests you is the Quote button. Also, another one of those buttons is very usefull, namely the Edit button, so you can edit your post instead of double posting.
    I should have thought about that before posting my question in the forum:mad:
    thanks! and i wont double post again i promise :P
    Last edited by D4rkNrG; 10-13-2010 at 09:17 AM.

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Don't beat yourself up about it, your question acctually was pretty valid, not a "here's my homework do it for me plz plz plz" code beg.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    Best read from the source of all Java rules:
    Classes
    8.3.1.2 final Fields
    A field can be declared final (4.12.4). Both class and instance variables (static and non-static fields) may be declared final.

    It is a compile-time error if a blank final (4.12.4) class variable is not definitely assigned (16.8) by a static initializer (8.7) of the class in which it is declared.

    A blank final instance variable must be definitely assigned (16.9) at the end of every constructor (8.8) of the class in which it is declared; otherwise a compile-time error occurs.


    db

Similar Threads

  1. about Final variable
    By Ramanjaneyulu in forum Advanced Java
    Replies: 6
    Last Post: 08-17-2010, 09:49 AM
  2. Replies: 2
    Last Post: 12-05-2009, 11:24 AM
  3. Constructor with final parameters
    By pm_4u in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 08:36 AM
  4. Using final with method arguments
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:48 PM
  5. Method with final keyword
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:39 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
  •