aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward Referencing Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward Referencing Doubt" Watch "Forward Referencing Doubt" New topic
Author

Forward Referencing Doubt

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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 ]

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Aug 03, 2002
Posts: 7729
"But in the above code the method is on the RHS which very well violated the DBRR"

The method is where?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Aug 03, 2002
Posts: 7729
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Barry,

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

Regards,
Jothi Shankar Kumar. S
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
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 !!


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Jothi, have you read Corey's Tipline article on Forward Referencing?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Thanks Barry,

Regards,
Jothi Shankar Kumar. S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward Referencing Doubt