Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTrevor Wood
17,828 PointsHaving trouble with this challenge, Java
I have the following code written but it keeps asking me to use the "keyword" which i'm assuming is "color", but I'm still not sure where I should put it.
Any ideas?
public class Example {
Object Gokart = new Object();
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
}
}
2 Answers
Michael Hess
24,512 PointsFirst you're declaring a goKart of type GoKart. There is a GoKart class that we cannot see in this exercise. That is where GoKart is coming from -- GoKart is an object type in this case.
Object obj = new Object(); is saying that we want to create an obj of type Object and instantiate a new Object().
// create object goKart of type GoKart
GoKart goKart;
Then we are instantiating a new GoKart object named goKart.
goKart = new GoKart();
We can do this all in the same line:
GoKart goKart = new GoKart();
GoKart accepts color in it's constructor and has a getColor() getter method that will get the color. So we are passing color to the constructor and the getColor method will give us the color if we ask for it.
It can be awkward to think about at first, but it gets easier to understand with practice! If you need me explain anything else - just ask!
Michael Hess
24,512 PointsHi Trevor,
Pass color to GoKart, then print the color using printf and the getColor method.
public class Example {
public static void main(String[] args) {
GoKart goKart = new GoKart("red"); //task 1
System.out.println("We are going to create a GoKart");
System.out.printf(goKart.getColor()); //task 2
}
}
If you have any questions I'll be more than happy to answer them!
Trevor Wood
17,828 PointsAwesome, thanks a bunch! I just don't understand why there's two "GoKart"s and one goKart.
Whenever I googled it, it showed that
Object Gokart = new Object();
was the way to make a new object.