• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forward Referencing Doubt

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Consider the following code,



Is this an illegal forward referencing as we violate the declare before read rule at Line 3? Can anyone explain this please?

Regards,
Jothi Shankar Kumar. S


(removed shout in title)
[ October 15, 2006: Message edited by: Barry Gaunt ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Sorry....In the above code please consider the return noOfRooms * occupancyPerRoom in the public int initMaxGuests() method and also create an object in the main method.

Regards,
Jothi Shankar Kumar. S
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code won't compile as is since you forgot to return an int from your initMainGuests method. hen you do return an int from that method the code will compile and run, but the result in my opinion is a bit surprising. Try running your code slightly modified so that it compiles and I changed a few of the printlns:



What would you expect the result to be?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best thing to do is to edit your initial post using the icon that looks like a piece of paper with pencil.

No, the use of occupancyPerRoom in the method does not violate the declare before read rule. But using the method where you do will not print the results that you may expect. The value of occupancyPerRoom printed and used in the calculation will be 0 and not 2. (You will have to give the method something to return to get it to compile).
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

I read that the declaration before read rule (DBRR) should not be violated. So as per that, the declaration can happen in the left hand side but not on the right hand side (RHS). But in the above code the method is on the RHS which very well violated the DBRR. Please anyone?

Regards,
Jothi Shankar Kumar. S
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"But in the above code the method is on the RHS which very well violated the DBRR"

The method is where?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

private int maxNoOfRooms = initMaxGuests(); //Line 3

In the above line the initMaxGuests() will return return occupancyPerRoom * noOfRooms; which is more or less like,

private int maxNoOfRooms = occupancyPerRoom * noOfRooms;...

So the occupancyPerRoom is read before declared???please can you explain on this.

Regards,
Jothi Shankar Kumar. S
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just a call of the method. The compiler does not see the call to the method and then go to see what the method does.

The code referencing the instance variables is in the definition of the method. At the time the method body is compiled all the instance variables have been defined (even if not fully initialized).
[ October 14, 2006: Message edited by: Barry Gaunt ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

I did not quite understand it. Please can yopu elaborate it furthur?

Regards,
Jothi Shankar Kumar. S
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi, did you try to run the code I posted? If not, try it... you'll see how the value of "occupancyPerRoom" is not what you might expect when it is first used in your method call.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rick,

I tried that same piece of code even before you posted. I waned to know what forward referencing is with respect to method returns.

Regards,
Jothi Shankar Kumar. S
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi,

Let me give a try here to summarize what Barry meant.

Reason Code compile without any error as Barry mentioned
"That's just a call of the method. The compiler does not see the call to the method and then go to see what the method does."

So when you write initMaxGuests() then compiler does not go and check body of method to verify what variables it is referring to.

Now, when you run this code, initMaxGuests() will return 0 reason again comes from Barry's statement below.

"At the time the method body is compiled all the instance variables have been defined (even if not fully initialized)."

Therefore value of occupancyPerRoom variable will be 0 rather than 2.Hence value that will be assigned to maxNoOfRooms will be zero.

I hope it helps !!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi, have you read Corey's Tipline article on Forward Referencing?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry,

Regards,
Jothi Shankar Kumar. S
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic