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?