File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Final keyword??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final keyword???" Watch "Final keyword???" New topic
Author

Final keyword???

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Why is it said that final variables have to be initialized before the constructor runs??? Moreover, it is also said that the value of final variables cannot be changed once assigned.

class Final1
{
final int x=3;

Final1()
{
final int x=2;
System.out.println("x:"+x);
}

public String go()
{
String s="JAVA";
System.out.println("String value:"+s);

return s;

}


}
public class Finaltest
{

public static void main(String[] args)
{
Final1 f=new Final1();
f.go();
}
}

But when i run the program the answer is x=3; According to me , there should be a compiler error for trying to reassign a final variable??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
Please find the code button, and please use proper indentation so we can better read the code.

A final field must be initialised before the constructor completes. And you are not reassigning a final variable, you are shadowing it with a local variable.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
what is the difference between shadowing and reassigning a final variable???
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146


Hey !

Final playes different roles at different places but its meaning never changes.

Keyword "final" - meaning in java vocabulary is "It cannot be further modified"

Role with <class>: It has a role with class as - A class declared final cannot be extended to modify / add new functionalities

ex : our String class - Refer java API Documentation

Role with <variable>: It has a role with variable as - The variable value is constant and never changes and allowed only once for initialization.

ex: final int a = 0;

Value never changes and is constant for all instances of the class !

Role with <methods>: method can never be overridden.






Here, possibly the declaration is at the class level.



Here, possibly the declaration is local to the constructor block.


According to me , there should be a compiler error for trying to reassign a final variable??


FALSE !
This is not re-assignment. A reassignment will not have a Second Declaration.

The variables vary by its block of initialization !

Also, the answer is 2 ! and not 3 !



Intialization of final variables is also possible within the Constructor block !

Strictly Speaking !Variables declared and defined inside the constructor block are never used
by any of the class member methods () !

Those are local to only that constructor block !

Those values are not accessible to instances of the class.






Try this code !




Output !

x:2
String value:JAVA
go:x :3


[ October 07, 2008: Message edited by: ram kumar ]

Discussion - the powerfull way to excellence!
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
hello,ram kumar,thanks for your reply.

Can you tell me, why go:x prints the value 3??
[ October 07, 2008: Message edited by: Abhi vijay ]
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Campbell Ritchie:
Please find the code button, and please use proper indentation so we can better read the code.

A final field must be initialised before the constructor completes. And you are not reassigning a final variable, you are shadowing it with a local variable.



But When i replace the code like




Output !

It gives me a compiler error saying x is not initialized !

Why is it not looking for the class variable (x) ! when already one final x=2 local to the constructor block has been initialized !


But i could say

" this.x=3; // works fine "


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
Ram Kumar: please find the code button too.

Abhi Vijay: there are all sorts of places where shadowing is described; the official definition is in the Java Language Specification; that is the index, so look for "S."
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Campbell Ritchie:
Ram Kumar: please find the code button too.

Abhi Vijay: there are all sorts of places where shadowing is described; the official definition is in the Java Language Specification; that is the index, so look for "S."



Hi Campbell !

Ram Kumar: please find the code button too.

Hope ! every code is used with code tags properly ! do you mean the indentation !
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Campbell Ritchie:
Please find the code button, and please use proper indentation so we can better read the code.

A final field must be initialised before the constructor completes. And you are not reassigning a final variable, you are shadowing it with a local variable.


Could some one answer for my POST !
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Abhi vijay:
hello,ram kumar,thanks for your reply.

Can you tell me, why go:x prints the value 3??

[ October 07, 2008: Message edited by: Abhi vijay ]


Also, the answer is 2 ! and not 3 !


Do you Agree !
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Abhi vijay:
hello,ram kumar,thanks for your reply.

Can you tell me, why go:x prints the value 3??

[ October 07, 2008: Message edited by: Abhi vijay ]


Since, there is no local variable x for method go() so it prints from the class variable x . as initialized in the constructor as x !

If there is a local variable x and still if you want the class variable x to be printed, you could just say - this.x

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
In the latest form of the Final1 constructor, you are declaring a local variable of the same name as the field, but unless (as Ram Kumar has correctly said) you write this.x, the local variable takes precedence over the field. So you are initialising a final local variable twice and not initialising the field at all.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Campbell Ritchie:
In the latest form of the Final1 constructor, you are declaring a local variable of the same name as the field, but unless (as Ram Kumar has correctly said) you write this.x, the local variable takes precedence over the field. So you are initialising a final local variable twice and not initialising the field at all.



That was cool over final variables -

What do you think ?

"decalring constant" means declaring static final or just final.

I feel like -

static final will keep a variable address constant and value constant among all instances (Hope ! It is not the right place to mention 'instances' here, because when i say static it becomes a class member )

final will keep only the value constant among all instances !

Am i Right !

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
No.

A final variable means its reference can be set up once and once only and not changed thereafter; if however the reference points to a mutable object, its contents and value can be changed.

Constants are used in the formatThey are the only case where it is respectable to use public access for variables. Since those are primitives or immutable objects, their location and value are fixed. You mark the fields static because their value is always the same and there is no point in having multiple copies of the same value.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Campbell Ritchie:
No.

A final variable means its reference can be set up once and once only and not changed thereafter; if however the reference points to a mutable object, its contents and value can be changed.

Constants are used in the formatThey are the only case where it is respectable to use public access for variables. Since those are primitives or immutable objects, their location and value are fixed. You mark the fields static because their value is always the same and there is no point in having multiple copies of the same value.




You mark the fields static because their value is always the same and there is no point in having multiple copies of the same value.


Hey Campbell,

I would understand that static fields will hold same value across all instance of the object ! and there is only one copy !

static case :


still can be changed as



prediction :
  • Can be changed as said above !
  • The refernce lies the same for 'i'[/B]


  • final case :



    prediction :
  • This value cannot be changed once initialized !
  • Am not sure about how many copies are maintained ! (hope ! one copy for each instance)


  • static final



    prediction :
  • since "static", reference is one (only one copy maintained);
  • since "final", value is one across all instance (initialized once !)



  • With respect to the copies maintained and the values !

    Am i right about the understanding of static / final / static final !

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36499
        
      16
    Originally posted by ram kumar:
    . . .

    still can be changed as



    prediction :
  • Can be changed as said above !
  • The refernce lies the same for 'i' That is a primitive; there is no reference to a primitive.

  • . . .

    Am i right about the understanding of static / final / static final !


    Apart from the minor point about primitives, yes.
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Campbell Ritchie:


    Apart from the minor point about primitives, yes.


    Thanks a lot ! Campbell.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36499
        
      16
    You're welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Final keyword???
     
    Similar Threads
    transient
    Mistake or misunderstaning on RHE?
    what can be static???
    Switch statement and constant variables
    Reverse 5 digit number by multiplying 4