Results 1 to 10 of 10
- 10-12-2010, 10:38 PM #1
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 :)
- 10-12-2010, 11:12 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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?
- 10-12-2010, 11:58 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
I shall have to admit, I never realised that could be done that way. Oh the simple things! :D
- 10-13-2010, 08:47 AM #5
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?
- 10-13-2010, 08:50 AM #6
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
- 10-13-2010, 08:56 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Just think about it, if you do this:
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.Java Code:int a;
Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-13-2010, 09:14 AM #8
- 10-13-2010, 09:55 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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-13-2010, 11:18 AM #10
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
-
about Final variable
By Ramanjaneyulu in forum Advanced JavaReplies: 6Last Post: 08-17-2010, 09:49 AM -
local variable tf1 is accessed from within inner class; needs to be declared final
By shoeb83 in forum New To JavaReplies: 2Last Post: 12-05-2009, 11:24 AM -
Constructor with final parameters
By pm_4u in forum New To JavaReplies: 3Last Post: 05-18-2008, 08:36 AM -
Using final with method arguments
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:48 PM -
Method with final keyword
By javaplus in forum New To JavaReplies: 2Last Post: 11-29-2007, 09:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks