views:

484

answers:

6

What are class, object and instance in the context of java?

Please illustrate your answer with examples.

+8  A: 

A class is basically a definition, and contains the object's code. An object is an instance of a class

for example if you say

String word = new String();

the class is the String class, which describes the object (instance) word.

Mustafa A. Jabbar
Did you mean that objects and instances are same?
Ryan
yes they're actually the same
Mustafa A. Jabbar
Thanks david for the link.From the topics I got thisEvery real world things which have state and behaviour can be called as "object". And to classify these objects we use class(A class is the blueprint from which individual objects are created).And it says that, the objects of the class are instances.Now please someone tell me what is the differences between object and instance?Does this mean that object don't really exist in context of programming and instance represents object in it?
Ryan
@Mustafa: I'm sorry to contradict you, but according to the JLS, an array is also an object in Java. And you'll find that the JLS does not define the term 'instance' at all. See my answer.
Stephen C
@Ryan: See my answer for the distinction between "instance" and "object". @Mustafa's answer and comment are (IMO) misleading.
Stephen C
+3  A: 

mustafabar's answer is good, but based on the beginner level of the question (no offence meant, I was a beginner once, too) I'd recommend some reading up. Here is a good place to start. http://java.sun.com/docs/books/tutorial/java/concepts/

David Stratton
+3  A: 

Java (and any other programming language) is modelled in terms of types and values. At the theoretical level, a value is a representation for some quantum of information, and a type is a set of values. When we say value X is an instance of type Y, we are simply saying that X is a member of the set of values that is the type Y.

So that's what the term instance really means: it describes a relationship not a thing.

The type system of the Java programming language supports two kinds of types, primitive types and reference types. The reference types are further divided into the Classes and array types. A Java Object is (according to the JLS) an instance of a reference type; i.e. either an array or an instance of a Class.

That's the theory. In practice, many developers treat the words "instance" and "object" as synonyms. (And that includes me then I'm trying to explain something quickly.)

Stephen C
I really appreciate your answer sir, and I almost got it, just one more question. We say-"Whenever the compiler hits the 0 argument constructor it creates a instance of a class.". In this context what is really created an instance or an object? An object I guess and we use the word "instance" as synonym.But it would kind, if you better confirm it.
Ryan
@Ryan: Basically yes: it is creating an Object which is an instance of the Class in question. (But note that the quoted sentence is saying "instance of a Class" rather than just "instance" ... so the author is not actually using "instance" as a synonym here.)
Stephen C
Thanks a lot for explaining.
Ryan
@Ryan: Incidentally, the phrase "the compiler hits the O arg constructor" is (IMO) very sloppy ... and incorrect if you read it literally. The compiler does not create instances of Classes, generated code does ... when executed.
Stephen C
A: 

If you have a program that models cars you have a class to represent cars, so in Code you could say:

Car someCar = new Car();

someCar is now an instance of the class Car. If the program is used at a repairshop and the someCar represents your car in their system, then your car is the object.

So Car is a class that can represent any real world car someCar is an instance of the Car class and someCare represents one real life object (your car)

however instance and object is very often used interchangably when it comes to discussing coding

Rune FS
Strictly speaking `someCar` is a reference to a Car instance. The instance itself doesn't have a name.
Joachim Sauer
A: 

Any kind of data your computer stores and processes is in its most basic representation a row of bits. The way those bits are interpreted is done through data types. Data types can be primitive or complex. Primitive data types are - for instance - int or double. They have a specific length and a specific way of being interpreted. In the case of an integer, usually the first bit is used for the sign, the others are used for the value.

Complex data types can be combinations of primitive and other complex data types and are called "Class" in Java.

You can define the complex data type PeopleName consisting of two Strings called first and last name. Each String in Java is another complex data type. Strings in return are (probably) implemented using the primitive data type char for which Java knows how many bits they take to store and how to interpret them.

When you create an instance of a data type, you get an object and your computers reserves some memory for it and remembers its location and the name of that instance. An instance of PeopleName in memory will take up the space of the two String variables plus a bit more for bookkeeping. An integer takes up 32 bits in Java.

Complex data types can have methods assigned to them. Methods can perform actions on their arguments or on the instance of the data type you call this method from. If you have two instances of PeopleName called p1 and p2 and you call a method p1.getFirstName(), it usually returns the first name of the first person but not the second person's.

Kage
+2  A: 

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

This is easy to understand if you look at an example. For example, suppose you have a class House. Your own house is an object and is an instance of class House. Your sister's house is another object (another instance of class House).

// Class House describes what a house is
class House {
    // ...
}

// You can use class House to create objects (instances of class House)
House myHouse = new House();
House sistersHouse = new House();

The class House describes the concept of what a house is, and there are specific, concrete houses which are objects and instances of class House.

Note: This is exactly the same in Java as in all object oriented programming languages.

Jesper