File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variable question from kb

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Dinesh Tahiliani
    Ranch Hand
    Posts: 486
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    which value is here than pass ??
     
    Christophe Verré
    Sheriff
    Pie
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    a copy of the final variable, not the final variable itself.
     
    Dinesh Tahiliani
    Ranch Hand
    Posts: 486
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In this case will be what 340L or 343L ???
     
    Christophe Verré
    Sheriff
    Pie
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First,


    And then


    With all the comment, can you figure out what is going to be output ?
     
    Dinesh Tahiliani
    Ranch Hand
    Posts: 486
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for excellent reply...
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic