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 trial

Java

A clear definition of "Instance"?

After learning java for a while, some of these terms are still a bit unclear. Is instance the same as a variable?

1 Answer

An instance is exactly that. Its a single instance of a class. A single existence that you can manipulate using the properties and methods of the class on which the instance is based. People use the words instance and object interchangeably for the most part.

Yes, an instance (AKA Object) of a class can be thought of as a variable in that we are assigning a value to a name. Its different from primitively typed variables in that an instance of a class is a "reference type" whereas primitives are "value types". If you want a more thorough explanation of what that means, you can see this video

In short, an object is a single instance of a class. The class is used as a blueprint for that instance. It lays out the types of properties and the different methods that object has. Then when the instance or object of that class is created, you can assign values to those properties and make it unique.

For example, say you had a Dog class. It has a string property called name. Using that dog class you can create multiple Dog instances, all with different values for that name property. You can have a dog named spike, a dog named John, and a dog named paul. They are all instances of that Dog class, however they have different values for the properties of that class. They exist independently of each other, but are all based on the same class and exhibit the same properties and behaviors

The class is the blueprint. The instance is the realization of that blueprint into something tangible, something more concrete. By creating an instance of a Dog class, you can go from "This is what a dog should look like (properties) and how it can behave (methods)" to "This is a Dog. His name is Paul. Lets take Paul for a walk by calling the walk method on on this particular instance of the dog class"

Thanks for your great answer!

So an instance of a class is basically an object that is created from a blueprint, or class, and the process is called instantiation?

Yes thats correct. Instantiation is the process used to create an object based on a class.