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

Forward reference in a method

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Hotel{

private int rooms = 12;
private int maxGuests = initMaxGuests(); // case 1

//private int maxGuests = rooms * occupancyPerRoom; //illegal fwd ref.

/*{ System.out.println("Initializer block: Occupancy per room "+occupancyPerRoom); //case 2 instance initializer block
}*/

private int occupancyPerRoom =2;

public int initMaxGuests(){

System.out.println("Occupancy per room "+occupancyPerRoom); //case 1
System.out.println("maxGuests "+maxGuests);
return rooms*occupancyPerRoom;

}

}

public class TestOrder{

public static void main(String[] arg){

Hotel hotel = new Hotel();


}

}


In this class, occupancyPerRoom is given in print statement in a method initMaxGuests. but it is not giving any compile error.
where as print statement in case 2, is giving compile error saying that it is an illegal forward reference.
Why is case 1 not giving any error though the variable is used in print statement before declaration?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an instance variable tries to forward reference another instance variable during initialization, the compiler will complain. Although this kind of forward referencing is disallowed by the compiler in an attempt to help programmers avoid just such kind of mistake, but you can't let down your guard completely.

Accesses by methods are not checked by the forward reference rule, that is a way you could inadvertently (or purposefully) circumvent the compiler's preventative restrictions
What Are the Forward Reference Rules?
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Accesses by methods are not checked by the forward reference rule

Is there any special purpose for not checking so?

I tried running some more examples in which i can access the field using this.fdname in non static initializer expressions. It is equivalent to accessing the field by its simple name in methods(in terms of value that the field contains). Why is this special form this.fdNAme allowed?

Thanks
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this.fdNAme" OK - not accessed via simple name
See example in 8.3.2.3 Restrictions on the use of Fields during Initialization.
 
mooooooo ..... tiny ad ....
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic