Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B Selftest Q.no:9 page no:263

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output is:343 341 340.

doubt is:
we declared local variable tooth as final variable.so this variable should not be changed.
How it has incremented in the body of method doIt(long tooth)?

can anyone clarify me?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method local variable tooth has shadowed the static tooth class variable.
[ November 03, 2008: Message edited by: Amit Ghorpade ]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ganesh,

When you are talking about this piece of code, just execute it
line by line.

class Knowing {
1-static final long tooth = 343L;
2-static long doIt(long tooth) {
3-System.out.print(++tooth + " ");
4-return ++tooth;
5-}
6-public static void main(String[] args) {
7-System.out.print(tooth + " ");
8-final long tooth = 340L; --------------line1
9-new Knowing().doIt(tooth);
10-System.out.println(tooth);
11-}
12-}

First output will be the instance variable having value '343'
when line 7 is encountered. So at this point the output is

343

Next, execution proceeds to line 8 where method main declares
a local variable by same name tooth. At this point, the class
variable is shadowed by the local variable tooth. Line 9 invokes
the method doIt() and passes its value to doIt. At this point
if you see line 2, doIt() signature declares a local variable or
parameter by name of tooth as well. At this point value of tooth
from main method is copied into it. The local has shadowed the
final variable, then at line-3 it increments the value to 341,so
at this point the output is like

343 341

After line 4, the method body is finished as s tooth was local to
doIt(), its life also ends. Then control is gotten back to line-9.
After line 9, at line-10, again the local variable tooth hides the
class variable tooth and prints out the value as 340, so the output
at this point is

343 341 340

Hope this clarifies your doubt.
Ben
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dear,

I have compiled your program. And i got the same answer, So i decompiled the code which as below. Which have cleared my dought.

import java.io.PrintStream;

class Knowing
{

Knowing()
{
}

static long doIt(long l)
{
System.out.print((new StringBuilder()).append(++l).append(" ").toString());
return ++l;
}

public static void main(String args[])
{
System.out.print("343 ");
new Knowing();
doIt(340L);
System.out.println(340L);
}

static final long tooth = 343L;
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is a pass-by-value trick, so easy to get caught here.
good quesiton. Thank you for post it :-)


Christina
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya its just a question on pass by value. Just understand that when a method is called by passing an argument, the thing passed is only the value. It is not the final variable we are passing. But just the value inside it.
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic