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: Adding the "final" keyword makes difference, I'm confused with the sequence flow.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool [Solved] Adding the "final" keyword confused me on the sequence flow.

    Codes:
    class Parent {
    	protected int value = 1 ;	
    	Parent() { 
                value = getNum();
            }	
    	public int getNum(){ 
                return 5; 
            }	
    	public int getValue(){ 
                return value; 
            }
    }
     
    class Child extends Parent {
       private int x = 4;     // Before line is executed, the value of "Parent.value" already assigned to be 10, why?
       private final int num = 10; //Remove the "final" here, result totally changed, why?
     
       public Child(){}
     
       public int getNum(){ 
          return num;  
       }
    }
     
    public class test
    {
      public static void main(String args[])
      { 
          Child cObj = new Child();
          System.out.println("cObj.getValue() = " + cObj.getValue());
      }
    }

    Remove the "final" keyword from the line "private final int num = 10;", the result prints out "cObj.getValue() = 0". I noticed that the Parents.value is assigned to 0 before Child.num is initialized to be 10 of the line "private final int num = 10;"; Strange....

    With the "final", runs with break point and breaks on line, private int x = 4;, the Parents.value is already assigned to 10, why is that?

    By running the program with "final" and without "final" using break point, the line of the code in sequence flow will be different?

    Also, I noticed the sequence flow will run differently when Parent.value is initialized and not initialized.
    I am very confused with the code traveling sequence, can anyone please explain that to me in very detail ?
    Last edited by SmokyBrain; May 3rd, 2012 at 03:55 AM. Reason: Result found and case closed.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Adding the "final" keyword makes difference, I'm confused with the sequence flow.

    The first call in a child class is to call the parent class constructor, initialization of non-final variables sets their values to default, and initialization to custom values occurs inherently within the constructor (see Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects) ). In affect, although not explicit this results in the following for a non-final variable (this might help you work out the workflow)

    class Child extends Parent {
       private int num;//defaults to 0 
     
       public Child(){
           super();
           num = 10;
       }
     
       public int getNum(){ 
          return num;  
       }
    }

    Adding final for initialization results in initialization of the variable prior to the constructor to a value different than the default value for the variable type. See the following article for a more thorough description
    Object initialization in Java - JavaWorld
    Last edited by copeg; April 29th, 2012 at 11:54 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding the "final" keyword makes difference, I'm confused with the sequence flow.

    Quote Originally Posted by copeg View Post
    The first call in a child class is to call the parent class constructor, initialization of non-final variables sets their values to default, and initialization to custom values occurs inherently within the constructor (see Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects) ). In affect, although not explicit this results in the following for a non-final variable (this might help you work out the workflow)

    Adding final for initialization results in initialization of the variable prior to the constructor to a value different than the default value for the variable type. See the following article for a more thorough description
    Object initialization in Java - JavaWorld
    Thank you copeg,

    By removing the final from the line "private final int num = 10;", the program tends to have "making forward references" problem.
    Child constructor -> Parent constructor -> initialization of parent's non-final variables, then goes into Parent's constructor body. I can understand these by reading the documents you mentioned.

    The part "Calling subclassed methods from constructors" also explains a little bit of the case that I am having.
    But, what about the code flow "walking"?

    "final" variables initialization goes prior to the constructor, I couldn't find it from the "Object initialization in Java" link above.

    Assume this case is true: the line "private final int num = 10;" is going to be executed TWICE ?
    I am traveling with the code line by line under debug mode, and when the line "private int x = 4;" is held, Parent.value is already assigned to be 10. Afterward, the debugger will just step into the next line "private final int num = 10;" and step out with no error; final "int type" variable being assigned a value twice?

    ? x 1000, Please Help......

Similar Threads

  1. Is "count" in this loop a keyword?
    By freakinawesome in forum Loops & Control Statements
    Replies: 5
    Last Post: April 25th, 2012, 04:25 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. declaring a "final" variable?
    By needleman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2010, 07:55 PM
  4. Why do we use the "this" keyword?
    By EmSaint in forum Object Oriented Programming
    Replies: 1
    Last Post: November 1st, 2010, 08:28 PM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM