wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can final variables instantiated after they declare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can final variables instantiated after they declare" Watch "can final variables instantiated after they declare" New topic
Author

can final variables instantiated after they declare

amalip epa
Greenhorn

Joined: Mar 19, 2005
Posts: 7
can final variables be instantiated after they declare?
like this
class X{
public void FinalTest() {
x=28;
System.out.println(x);
public static void main (String ar[]){
X.FinalTest();
}
}
Ramana Uppala
Greenhorn

Joined: Jan 04, 2005
Posts: 16
Final Variables must be initialized during declaration.One other way is we can assign a value to a final variable in class constructor.

Class sample{
final int i;
sample(){
i=10; //This is legal
}
}
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
Can you give the full code please beacuse ure code doesnt compile
you havent declared x, and your method Final test also includes your main method ..is that really the code..
I thnk you shud go thru the code again I dont thnk it even has a final declaration ..unless there is someething there that i havent noticed
Cheers
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
HI Guys

Have this simple code with .....

class X
{
final int x;
X() { x=10; } //Constructor
public static void main (String [] args)
{
new X();
}
}

I hope this clears you
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
Also if u just wana kno abt final variables
The can be left unintialzed & these are known as blank final varaibles.
But if these belong to your class as opposed to method(Local) they must have a value before your class terminates else u will get a compilation error..

But if these are local final variables they can be left unitilazed you will not get a compiler error ..But u must initialze them b4 u use them..

Needless to say once they have a value these values become final that is u cannot change them.
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
Case 1
Originally posted by Nikhilesh Fonseca:
Also if u just wana kno abt final variables
The can be left unintialzed & these are known as blank final varaibles.
But if these belong to your class as opposed to method(Local) they must have a value before your class terminates else u will get a compilation error..

Case 2


But if these are local final variables they can be left unitilazed you will not get a compiler error ..But u must initialze them b4 u use them..

Needless to say once they have a value these values become final that is u cannot change them.

why the difference in the 2 cases. i mean it should be that if u dont use blank final variables why bother with assignment?? on same grounds that i dont care when i declare local variable and dont use it.
So why the compiler is unhappy when i cheat final instance variables when it allows me to cheat local final variables...
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
because local variables by default reamin uninitalized whereas the others take some kind of default value as u may kno
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can final variables instantiated after they declare
 
Similar Threads
implicit and explicit casting
Question
variable access in inner class
Inner classes(SCJP 1.5)
Non-final member variables in interfaces