Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable question from kb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable question from kb" Watch "final variable question from kb" New topic
Author

final variable question from kb

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Rancher,

Need your help in this

class Knowing
{
static final long toth = 343L;
static long doit(long tooth)
{
System.out.println(++tooth);
return ++tooth;
}

public static void main(String [] a)
{
System.out.println(tooth);
final long tooth=340L;
newkKnowing().doit(tooth);
System.out.println(tooth);
}

}
o/p 343,341,340

Can anyone expalin me this
How can final variable been modified


Thanks<br />Dinesh
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Dinesh, after 349 posts, it's time you learn the beauty of Code Tags. Please use these whenever posting code.

There are 3 different variables called "tooth" in this example:
  • A final variable in the class Knowing.
  • A non-final local variable in the method doit.
  • A final local variable in the method main.
  • Keep in mind that whenever you pass a primitive to a method, you are passing a copy of that value, which is assigned to a local variable.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Dinesh Tahiliani
    Ranch Hand

    Joined: Aug 06, 2007
    Posts: 486
    which value is here than pass ??
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    a copy of the final variable, not the final variable itself.


    [My Blog]
    All roads lead to JavaRanch
    Dinesh Tahiliani
    Ranch Hand

    Joined: Aug 06, 2007
    Posts: 486
    In this case will be what 340L or 343L ???
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    First,


    And then


    With all the comment, can you figure out what is going to be output ?
    Dinesh Tahiliani
    Ranch Hand

    Joined: Aug 06, 2007
    Posts: 486
    Thanks for excellent reply...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: final variable question from kb
     
    Similar Threads
    doubt in shadowing
    K&B Selftest Q.no:9 page no:263
    Doubt in final variable
    Variable shadowing
    assignment error