• 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

I am confused

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



/*
Value of a is:0
Value of A is:2
Value of a is:0
Value of A is:12
ob1.a:2
ob2.a:12
Value of a is:0
Value of A is:22
ob2.a after second increment22
*/
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try tracing through the code like this:

- Line 18 creates a new Test, passing in i = 2. That prints the initial value of a (= 0), then adds 2 to that and prints that. So a = 2.
- Line 20 calls incr() on that first Test. This creates a new Test, passing in i = 12. Again, this prints the initial value of a (remember it's a new object, so it's zero again), then adds 2 to that and prints that. So a = 12.
- Etc. Remember that each call to incr creates a new Test object.

(I think you're missing part of the output)
 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi,mathew.
First,I want to thank you.I am new to java,and confused.
But you are genius and a good teacher also.
Thank you,again.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome .
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're going to want to spend some time -- a couple months, at the least -- with a beginning Java book, before you jump into the deep end studying SCJP. Otherwise you are going to be very, very frustrated.
reply
    Bookmark Topic Watch Topic
  • New Topic