Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

going bananas with this one

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

The reference says that x on line 2 is not the same as x on line 4 and that's what I'm curious about. main() is a static method. Does that mean that x on line 4 is a static variable? I was taught that static members have no knowledge of any instance so if that's the case how can you even put a constructor inside a static method as shown above? I can see that it all compiles but it seems like a big contradiction to the separation of instances and statics. Line 5 has a constructor and line 8 has an instance initializer block which runs every time an instance is created right? If that's the case then x should be 4 by the time you compile line 5. Finally you hit line 6 and there is a preincremented x within. Is that x a local variable?



code sample courtesy of tmn.sun.com
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. At line 2 you are defining a class instance variable called x.
2. At line 4 you are defining a method local variable called x.
3 At line 5 you are not "putting a constructor inside a method". You are creating an instance of Banana. static methods can create object instances.
4. You have no static variables.
5. Instance initializer blocks execute at runtime - not at compile time..
6. I assume you mean line 10 when you talk about the "preincremented x". That is incrementing the class instance x (line 2). The go() method does not have access to the local variable in main(). It only has access to the class instance x.
 
Marshal
Posts: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this.

Get Campbell's requisite set of hardware, as recommended to our undergraduates. Pencil paper and rubber/eraser. The last is by far the most important
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic