• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

final variable question from kb

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    which value is here than pass ??
     
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    a copy of the final variable, not the final variable itself.
     
    Dinesh Tahiliani
    Ranch Hand
    Posts: 486
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In this case will be what 340L or 343L ???
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for excellent reply...
     
    I am Arthur, King of the Britons. And this is a tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic