This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt on Chapter 4 #7 question by OCA Jeanne Boyarsky and Scott Selikoff

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line numbers in main generate compile error?



A.None,the code compiles fine
B.Line 23
C.Line 24
D.Line 25
E.Line 26
F.Line 27

Answers are B,C,D,F

My doubt is in how will the compiler identify room as an object of Classroom while accessing roomNumber,floor,teacherName ,when it has failed to instantiate Classroom object room ? Can anybody please explain how Compiler thinks in this particular example?

Thanks,
Veena
 
Marshal
Posts: 64998
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Veena Pointi wrote:. . . Answers are B,C,D,F . . .

Please tell us what the “correct” and “incorrect” answers are.
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Veena Pointi wrote:My doubt is in how will the compiler identify room as an object of Classroom while accessing roomNumber,floor,teacherName



It does that because the variable room is declared to be of type Classroom. That happens here:



when it has failed to instantiate Classroom object room ? Can anybody please explain how Compiler thinks in this particular example?



The compiler doesn't "fail to instantiate" anything. It doesn't even try to instantiate objects -- that happens at run time. As I said, the compiler sees that room is declared to be of type Classroom. You can see that too, without having to execute any code.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: Please tell us what the “correct” and “incorrect” answers are.



Sorry.Edited the post to include answers.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:The compiler doesn't "fail to instantiate" anything. It doesn't even try to instantiate objects -- that happens at run time. As I said, the compiler sees that room is declared to be of type Classroom. You can see that too, without having to execute any code.



Nice explanation. So is it right to understand that compiler sees code the way we see except for RunTimeExceptions ?
 
Campbell Ritchie
Marshal
Posts: 64998
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is line 5 (answer B)? I can't see anything wrong with it.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am so sorry .My mistake in mapping line numbers from book .It is actually line 23.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!