The non-static toString () method cannot refer to a static context

No code needed, just some kind of guidance. I would like to maintain academic integrity in tact;)

I keep getting this annoying mistake. I need to call the toString method for each room instance. Any suggestions? I would prefer an answer within 2 hours, if at all possible.

public class Hotel { //constant public static final int NUM_ROOMS = 20; //variables public Room[] theRoom; public String name; public int totalDays; public double totalRate; public int singleCount; public int doubleCount; public int roomsRented; public int NOT_FOUND; public Hotel(String newName) { name = newName; Room[] Rooms = new Room[NUM_ROOMS]; } public double getTotalRentalSales() { return totalRate + roomsRented; } public double getAvgDays() { return roomsRented/totalDays; } public double getAvgRate() { return totalRate/roomsRented; } public int getSingleCount() { return singleCount; } public int getDoubleCount() { return doubleCount; } public String printRentalList() { System.out.println("Room Information: " + Room.toString()); } } 
+4
source share
4 answers

You should not try to call toString() on the class of the room, but rather on the Room object. In this method, scroll through the room array with the for loop and print the line returned by calling toString() for each Room object stored in the array, as this looks like what your method looks like.

for instance

 System.out.println("All Foos held here include: "); // using a "for-each" loop, assuming an array called fooArray that holds Foo objects for (Foo foo: fooArray) { System.out.println(foo); } 

You will obviously have to change the types and names of variables for your code.

Edit 2: although you will have to use the standard loop for the loop, and not for each loop, since you will not iterate over the entire array, but rather stop working when the number of numbers is reached.

 System.out.println("All Foos held here include: "); // using standard for loop, assuming an array called fooArray that holds Foo objects for (int i = 0; i < someMaxNumber; i++) { System.out.println(fooArray[i]); } 
+4
source

Since the error is already a state, do not call the instance method in a static context.

A room is a class, not an object. toString is an instance method. So Room.toString () in this case, the compiler is looking for a static toString method. But toString is an instance method, so it causes a problem.

Always remember that instance methods are called with the class object, not with the class itself.

0
source

What you are probably doing is calling toString() in the Room class, not an instance of it. For example, instead of writing:

 Room.toString() 

records:

 Room r = new Room() r.toString() 
0
source

Look at the following code, you can compile toString with a static variable without a new object, just throw an exception at runtime

 demo>cat Test.java class Water { public String toString() {return "whatever";} } public class Test { static Water water; public static void main(String...args) { System.out.println(water.toString()); } } demo>javac Test.java demo>java Test Exception in thread "main" java.lang.NullPointerException at Test.main(Test.java:8) 
0
source

Source: https://habr.com/ru/post/1448053/


All Articles