-1

I think I have my method set properly to give me the total number of rooms but it won't compile. Where did I go wrong?

public class Hotel {


    public static int singleRooms;
    public static int doubleRooms;
    public static int kingRooms;

    public Hotel(int numberSingleRooms, int numberDoubleRooms, int numberKingRooms) {
        singleRooms = numberSingleRooms;
        doubleRooms = numberDoubleRooms;
        kingRooms = numberKingRooms;
        }

    public static Hotel IvanHotel = new Hotel(1,3,5);


    int totalRooms = GetTotalRooms(IvanHotel);

    public static void main(String[] args) {
        System.out.println(totalRooms);
    }

}

public class GetTotalRooms {

    public static int totalRooms;

    public int GetTotalRooms(Hotel yourHotel) {

        totalRooms = Hotel.singleRooms + Hotel.doubleRooms + Hotel.kingRooms;

        return totalRooms;

    }


}

What should I change in order to get it to compile? I am getting a cannot find symbol error and I don't know why.

Ignatius
  • 2,745
  • 2
  • 20
  • 32

2 Answers2

1

Looks like you confused with the concept of static. You can't access non-static values from static context.

This code should fix things up

public class Hotel { 
    //...
    static int totalRooms = GetTotalRooms.getRoomsCount(IvanHotel);
    //...     
} 

class GetTotalRooms { 

    public static int getRoomsCount(Hotel yourHotel) {
        totalRooms = Hotel.singleRooms + Hotel.doubleRooms + Hotel.kingRooms;

        return totalRooms;
    } 
} 

If you are a beginner I would suggest reading a lot of sample codes and start with basic concepts. Don't skip the basic concepts.

Doc
  • 10,831
  • 3
  • 39
  • 63
0

You need to do a bit more reading to understand what the purpose of static is and why you'd use it. The way you wrote your code, you cannot call a non static method in a static way, you first need to create an instance of your GetTotalRooms first before you can do that GetTotalRooms so in the case of your code you'd write this

//...
private GetTotalRooms getTotalRooms = new GetTotalRooms();
int totalRooms = getTotalRooms.GetTotalRooms(IvanHotel);
//...

or you should follow what Doc said and make the method your calling static as well.

I'd also recommend you practice your naming conventions, 1st your class name sounds like a method name and then you have a method with the exact same name of your class, your Hotel class is fine but the GetTotalRooms class is the problem,

id rather name it something like RoomsList since you want to return the total amount of rooms you have in your Hotel

your class names should refer an Object, GetTotalRooms is not an object it's an equation in your case. Then your method name should refer to an action, and it should start with a lower case character, the name you chose for your method is good, it tells me exactly whats going to happen when I call it, just make it start with a lowercase so that it won't look like a class.

you can read more about it here:

Coding like Shakespeare

and

dzone

Christiaan
  • 639
  • 1
  • 8
  • 18