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 trialJae Kim
3,044 PointsJava Objects - Can anyone explain this line of code to me please?
GoKart jaeKart = new GoKart("Red");
I am getting confused on the objects section in the Java track. Is it possible for someone to affirm or explain why my thinking is wrong or right? Your help is appreciated! :)
1) So is the "GoKart" in the beginning line the class?
2) Is 'jaeKart" the new object that is being created?
3) Lastly, what is the purpose of the "new" keyword. I remember it mentioned in the video but did not understand the purpose in writing "new GoKart".
2 Answers
Evan Demaris
64,262 PointsHi Jae,
The first GoKart is the type of variable you're making, in this case the type being an instance of the GoKart class. jaeKart is the name of the variable, and new
indicates that we are creating a brand new instance of GoKart with the argument of 'Red'.
Hope that helps, let me know if you have more questions about java objects!
Jae Kim
3,044 PointsThank you so much Evan! The information you provided has provided a lot of clarity as to what I was confused about. I apologize for all the inquiries. OOP seems very hard to learn, but I do enjoy the challenge!
Evan Demaris
64,262 PointsDon't worry too much about asking questions, for the most part other students are going to be happy to answer questions like yours because it makes us think about our own understanding of how the language works (and sometimes makes us look up concepts we can't describe well).
Keep pushing forward!
Jae Kim
3,044 PointsJae Kim
3,044 PointsThank you Evan!
So, essentially GoKart is the object type and jaeKart is the name of the variable. When I use the "new" keyword - I am creating a new instance of the GoKart class and then assigning it to the variable jaeKart?
I am probably thinking too much, but what does it mean to create a new instance of a class? Or, in this case being a new instance of "GoKart."
Evan Demaris
64,262 PointsEvan Demaris
64,262 PointsYou're welcome! Yes, you're correct to your first question. For your second question, creating an instance of any class means that you are making an 'object', which is a combination of variables, functions, and data structures, based off the class. The object can have specific values for the variables or functions available to it based off arguments taken by a constructor, or can have default values specified inside it. You can make objects to specify specific carts in this example, like in a race you might want to have an object for each different color of cart, or give it a 'name' argument/variable, things like that - objects are pretty versatile.