Results 1 to 7 of 7

Thread: Local Finals

  1. #1
    Drun's Avatar
    Drun is offline Member
    Join Date
    Nov 2009
    Location
    Indian Head, MD (Missing Pittsburgh PA)
    Posts
    44
    Rep Power
    0

    Question 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
    Java 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);
    	}
    
    }
    (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.
    Michael P. O'Connor
    http://www.mikeoconnor.net

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    > 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 07:15 AM.

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    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

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by pbrockway2 View Post
    > 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.
    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).
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by CodesAway View Post
    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).
    You and my compiler will have to agree to differ! "the following" in the original post is a class with a single member method lacking a return type.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    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

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. Are Local variables thread safe ?
    By samson in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-21-2010, 03:34 PM
  2. Transient & Local
    By Supamagier in forum Advanced Java
    Replies: 5
    Last Post: 04-28-2009, 03:35 AM
  3. Local IP
    By andrewms in forum Networking
    Replies: 5
    Last Post: 04-25-2009, 02:54 AM
  4. Getting address of Local Machine
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-24-2007, 08:38 PM
  5. Updating Local Content
    By ibanez270dx in forum Java Applets
    Replies: 1
    Last Post: 11-02-2007, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •