• 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

getInt()

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

I tried running the program it gave an output of :
1 20
Can any one let me know what is the getInt() method return.
I feek that the value of k should 21.
Please explain..
Sonir
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,
At the moment when program calls GetInt() value of k is 0 (default for int)
so printout is:
1 (as 0+1 = 1) and 20
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no clue what this explanation means. Could someone please elaborate?
Thanks!
-Paul
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The line int i= getInt() is initializer code. At the time this line executes, k does not have any value assigned to it. As an int declared outside a method, it is 0 by default.
So get int returns 0 + 1 .
HTH
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shivaji,
The line int i= getInt() is initializer code. At the time this line executes, k does not have any value assigned to it. As an int declared outside a method, it is 0 by default.
So get int returns 0 + 1 .

I'm still a little unclear. I realize k would have a default value but the first portion returned is "t.i" I dont see "i" anywhere?? The second portion returns "t.k" which returns 20 makes sense.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean k inside getInt() method is different from the variable k defined in the class TestClass ???

Originally posted by Shivaji Marathe:

The line int i= getInt() is initializer code. At the time this line executes, k does not have any value assigned to it. As an int declared outside a method, it is 0 by default.
So get int returns 0 + 1 .
HTH

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line int i= getInt() is initializer code. At the time this line executes, k does not have any value assigned to it. As an int declared outside a method, it is 0 by default.
So get int returns 0 + 1 .

I can see your point that K doesnt have a value, but since: int i = getInt() -- invoking the method, how is "i" actually 0 (incremented to 1) ??
TIA
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
just my 2 cents...
i knew this problem & solution and the reason.
actually we should avoid this kind of referencing. here, even if we have int k declared after getInt() method it would work !! which is surprising. it surprised me also.
the reason it works because k is simple 'int' type. so when first int i is loaded, getInt() is called and method getInt() certainly knows that there is a var called 'k' which is of int type (as there is a .class file generated for the class and so JVM's method area has to know that int k is there). now, as of when getInt() is called k is not yet initialized by the initializer and by default any int has value ZERO. so, getInt() gets value ZERO for 'k'.
but after that initializer initializes 'k' to some other value.
the reason why i recommend to avoid this kind of access is, here 'k' is int which by default gets some value but if its a reference then it's problem as by default it is null and if we are trying to get some values from that ref object in getInt() method then it will throw NullPointerException as the ref is not yet initialized or allocated memory.
here is the code for that,

try to run it.
regards
maulin.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maulin,
I appreciate you clearing that up for me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic