Results 1 to 7 of 7
Thread: Local Finals
- 11-20-2009, 05:41 AM #1
Local Finals
In java I have notised the following is legal
Why is this legal, why is it not illegal, why when you creat what I would think (comming from C++) would simply be a constant (const in C++) be allowed to be 1) created final with out assining a value (if it was an attrabute of the class there would have been an error, but not as a local varable) 2) why can I later assine it to something else see this other code belowJava Code:public class someclass { public somerandomefunction() { final someclass aRefrence; // this is the first part of the question /* Enter any amount of randome code here/coments what have you */ aRefrence = new someclass(); /* WTF shoudl not the first line in the question caused a compilation error */ } }
(yes I know stupid var names and bad naming convention but this is just playing around with the language to see what it can do.) the above code also compiles and run fine it produces the following outputJava Code:class clubsoda { int myValue; void setsomevalue(int value) { myValue = value; } public void myTest(clubsoda somerandomedrink) { final clubsoda hold; // = new Tea(); clubsoda newdrink = new clubsoda(); newdrink.setsomevalue(4); if(somerandomedrink != null) { hold = somerandomedrink; System.out.println("hold now is = to somerandomdrink"); } else { hold = new clubsoda(); System.out.println("hold now = to a new clubsoda"); } hold.setsomevalue(0); clubsoda saveTheDrinks; saveTheDrinks = newdrink; newdrink = new Tea(); newdrink.setsomevalue(5); saveTheDrinks.setsomevalue(2); } } public class Tea extends clubsoda{ public static void main(String arg[]) { clubsoda tea = new Tea(); clubsoda bland = new clubsoda(); bland.myTest(new clubsoda()); bland.myTest(null); } }
so I can say I do not belive that there is a fancy compiler trick going on that moves the assiment to the final, as in the case of the function myTest it could be anything or it could be null we don't know till we get to the function, so this has to be done at run time. So what is going on? why can I declare a local varable/refernce as final but not assing it till later.Java Code:hold now is = to somerandomdrink hold now = to a new clubsoda
Michael P. O'Connor
http://www.mikeoconnor.net
- 11-20-2009, 06:11 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
> In java I have notised the following is legal
Actually the example you give will not compile. If you're fixing it up to express whatever it is you mean, consider using standard cAsE cOnVeNtIoNs.
The variable in question is a blank final as described in the Java Language Specification. Chapter 16 of the JLS describes how the blank final variable must be "definitely unassigned" at the time of assignment. Other than that there is no restriction on assigning to it.Last edited by pbrockway2; 11-20-2009 at 06:15 AM.
- 11-20-2009, 06:11 AM #3
A final variable is not constant (like in C++). Rather, a final variable is one where you can set the reference once (and ONLY once).
As you probably found out, if you uncomment your line that initializes "hold", then you WILL get a compile error, since "hold" cannot be assigned (since it is final). Also, like your example shows, a final variable does not have to be initialized on the same line it is declared (again, this is different than a const in C++, where it does).
Another difference, correct me if I'm wrong (it's been a while since I've done C++ code), is that a const variable can only be used in methods that take a const variable. This is because in C++, a "const" variable is a TRUE constant - cannot change the reference, OR any of the fields in the object. In Java, only the reference is constant.
"Final" in Java is similar to a constant pointer in C++. The pointer is constant, but the value which the pointer references is not (that is, the fields can change). They call it a "final" value, because the reference cannot change - it's "final" in that once it's assigned, it's stuck (you cannot change it).
However, it's not a constant, since you can change the fields, AND you can initialize it at any point - that is, you do not have to initialize it one the same line it is declared (like in your example shows).CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-20-2009, 06:14 AM #4
- 11-20-2009, 06:20 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
- 11-20-2009, 06:28 AM #6
Oh, the first one, no of course not. That's meant to be psudocode, a "generic" idea, with a compilable example to follow.
Sorry, for my misunderstanding.CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-20-2009, 06:50 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Yes - I was just being picky. But with a point to make.
The OP's question and motivation are perfectly valid. However if you are going to delve into the minutiae of a language (and especially comparing one language with another) then you do have to be precise. (And concise, and legible.) The JLS devotes an entire chapter to the question of when blank finals can be assigned to, and reading that is worth any amount of "playing around" with random code.
Similar Threads
-
Are Local variables thread safe ?
By samson in forum Threads and SynchronizationReplies: 6Last Post: 12-21-2010, 02:34 PM -
Transient & Local
By Supamagier in forum Advanced JavaReplies: 5Last Post: 04-28-2009, 02:35 AM -
Local IP
By andrewms in forum NetworkingReplies: 5Last Post: 04-25-2009, 01:54 AM -
Getting address of Local Machine
By Java Tip in forum Java TipReplies: 0Last Post: 11-24-2007, 07:38 PM -
Updating Local Content
By ibanez270dx in forum Java AppletsReplies: 1Last Post: 11-02-2007, 01:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks