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 trialyadin michaeli
Courses Plus Student 5,423 Pointsconfused about that instance thing
Hi.
i dont get it... what is exactly an instance? it really confused me
can somebody explain it to me please?
4 Answers
Benjamin Sonin
12,938 PointsHi Yadin,
An 'instance' is basically the application of a class. When you create an object in Java, you are making an 'instance' of a class. This object contains the methods and member data defined within the class. Think of it in terms of something like a car. Imagine you had a class where you defined the attributes of a car. It has a method to accelerate, a method to brake, member data describing its name & model, resell value and its horse power. But just because you have a car defined and know what it is doesn't mean it present in your world. When you see a car on the road however, maybe a Toyota or a Lexus, you are seeing an instance of a car. This is like an object in your code, an instance of the class you've defined.
Benjamin Sonin
12,938 PointsThe instance would be the car. Toyota or Lexus might be member data on the car, like a String for its make and model.
You could instead have another class specifically for a Toyota, and then a Toyota object would be an instance of the Toyota class.
yadin michaeli
Courses Plus Student 5,423 Pointswhen we type : Something something = new Something can you tell me what the instance here? or this whole code is an instance?
Benjamin Sonin
12,938 PointsIf I have a class Car and make a new Car object called myFirstCar, it might look like this:
Car myFirstCar = new Car();
Here, myFirstCar is an object of the class Car. Thus, myFirstCar is an instance of class Car.
yadin michaeli
Courses Plus Student 5,423 Pointswow man thank you very very much i really appreciate you make my brain get a light up! :)
Robert Jenkins
594 PointsHi since the instance here is myFirstCar, what would new Car () be ?
yadin michaeli
Courses Plus Student 5,423 Pointshi i think is where you create new car...imagine that you go buy a car and tell the seller what car you want (class) and then in main you create the actually Car myFirstCar = new Car.
the seller give you the new car
make sense?
yadin michaeli
Courses Plus Student 5,423 Pointsyadin michaeli
Courses Plus Student 5,423 Pointsso the instance is actually the type of the car like Toyota or Lexus?