Local Finals

In java I have notised the following is legal

Java 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 */
}
}

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 below

(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 output

Java Code:

hold now is = to somerandomdrink
hold now = to a new clubsoda

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.

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.

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).

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.

No, it compiles, like it should. It's compiles because "hold" is initialized before it is used, which is the requirement for any object (final or otherwise).

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.