aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes getInt() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "getInt()" Watch "getInt()" New topic
Author

getInt()

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

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
Stov
Greenhorn

Joined: Jan 21, 2002
Posts: 3
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
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
I have no clue what this explanation means. Could someone please elaborate?
Thanks!
-Paul
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203

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

Joined: Jan 17, 2002
Posts: 172
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.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jan 17, 2002
Posts: 172
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jan 17, 2002
Posts: 172
Maulin,
I appreciate you clearing that up for me.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: getInt()