• 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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: 70654
288
  • 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: 25950
69
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: 70654
288
  • 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.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic