Why it wont let me create a object in the main method 13/13


#1



https://www.codecademy.com/en/courses/learn-java/lessons/object-oriented-programming/exercises/generalizations-oop-java


I typed the proper object into the main method " String myOrder = new Coffee(); " but after i run it says "Coffee.java:15: error: incompatible types: Coffee cannot be converted to String
String myOrder = new Coffee();
^
1 error " PLSS HELP ME SOLVE THIS ISSUE


class Coffee {
	
	public Coffee() {
    
	}
	
	public void addSugar(int cubes) {

		System.out.println("You added " + cubes + " sugar cubes.");

	}

	public static void main(String[] args) {
    
    String myOrder = new Coffee();

	}
}


#2

I have the same problem. what even is the main method?


#3

Hey.

Sorry if this reply is too late. But when you create an object of Coffee you need to give the variable myOrder a suitable datatype to go with it. When we create our own classes we're essentially creating our own user defined data types. Therefore we need to use Coffee instead of a String. And this makes sense because the compile error threw "Coffee cannot be converted to String". Coffee can not be coverted to a string because Coffee is of type Coffee.

So it should look like this:

Coffee myOrder = new Coffee();

#4

The main() method is the very first method called by the java run time environment. It serves as the entry point for the rest of the program so that other methods and various other tasks can be carried out.


#5

Thank you angus beef for the help really means a lot that some one helped :smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.