Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 457
  • 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: 64473
225
  • 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.
 
Marshal
Posts: 24461
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: 457
  • 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: 457
  • 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: 64473
225
  • 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: 457
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!