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

jiris mock question

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



I have got two errors.

Chapter3.java:74: cannot resolve symbol
symbol : method method (java.lang.String)
location: class Test
test.method("0");
^
Chapter3.java:81: cannot resolve symbol
symbol : variable str
location: class Test
System.out.println(t.str);


1)i have no idea why this line1 is not compiling .
please help me to correct the preceding errors?

2)what is the result of this program.i thought,this would print 0,0,0.Any ideas?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva,

Where is definition for class Test? That's why the error is!


Thanks and Regards,
cmbhatt
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra, Thanks.one more doubt

why t.str is displaying null insteadof 0 .Can you please give explanation of this?

when i make t as an instance variable,it displayed statckoverflowerror at runtime.what is going on behind this?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you meant to name it Chapter3 and not Test.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when t is a static reference, it refers to an instance of Chapter3. However, str is an instance variable that is not initialized by a constructor, so it is given its default value.

The reason you get a StackOverflowError when you make t an instance reference is that when a new instance of an object is created, its instance variable initializers are executed before the constructor is executed.

So by having the instance creation statement in the instance variable initializer, you basically have an infinite loop. Eventually the stack will be full.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



why t.str is displaying null insteadof 0 .Can you please give explanation of this?

when i make t as an instance variable,it displayed statckoverflowerror at runtime.what is going on behind this?



String str; (member variable of the class Chapter3)
Its default value is null not 0. So when you used t.str obviously it will print null and not 0.

StackOverFlowException:
Are you doing something like this:


The instance initializer executes before the constructor.

Try this code too to have idea...


Thanks and Regards,
cmbhatt
 
For my next trick, I'll need the help of a 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