What exactly is an instance in Java?

297,555

Solution 1

An object and an instance are the same thing.

Personally I prefer to use the word "instance" when referring to a specific object of a specific type, for example "an instance of type Foo". But when talking about objects in general I would say "objects" rather than "instances".

A reference either refers to a specific object or else it can be a null reference.


They say that they have to create an instance to their application. What does it mean?

They probably mean you have to write something like this:

Foo foo = new Foo();

If you are unsure what type you should instantiate you should contact the developers of the application and ask for a more complete example.

Solution 2

"instance to an application" means nothing.

"object" and "instance" are the same thing. There is a "class" that defines structure, and instances of that class (obtained with new ClassName()). For example there is the class Car, and there are instance with different properties like mileage, max speed, horse-power, brand, etc.

Reference is, in the Java context, a variable* - it is something pointing to an object/instance. For example, String s = null; - s is a reference, that currently references no instance, but can reference an instance of the String class.

*Jon Skeet made a note about the difference between a variable and a reference. See his comment. It is an important distinction about how Java works when you invoke a method - pass-by-value.

The value of s is a reference. It's very important to distinguish between variables and values, and objects and references.

Solution 3

When you use the keyword new for example JFrame j = new JFrame(); you are creating an instance of the class JFrame.

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.
Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Take a look here
Creating Objects


The types of the Java programming language are divided into two categories: primitive types and reference types.
The reference types are class types, interface types, and array types.
There is also a special null type.
An object is a dynamically created instance of a class type or a dynamically created array.
The values of a reference type are references to objects.

Refer Types, Values, and Variables for more information

Solution 4

I think that Object = Instance. Reference is a "link" to an Object.

Car c = new Car();

variable c stores a reference to an object of type Car.

Solution 5

Computer c= new Computer()

Here an object is created from the Computer class. A reference named c allows the programmer to access the object.

Share:
297,555
priya
Author by

priya

Updated on July 09, 2022

Comments

  • priya
    priya almost 2 years

    What is the difference between an object, instance, and reference? They say that they have to create an instance to their application? What does that mean?

  • Voooza
    Voooza about 13 years
    I don't think that j is an object. It just stores reference to an object.
  • Jon Skeet
    Jon Skeet about 13 years
    Note that j isn't even a reference, either. It's a variable. The value of j is a reference. It's very important to distinguish between variables and values, and objects and references.
  • Voooza
    Voooza about 13 years
    I said stores reference not is reference. Maybe I don't express myself well, but thats what I meant.
  • Alpine
    Alpine about 13 years
    @Jon I had said j is the object that is created of the type JFrame. By that I mean jis a variable of reference type which is a JFrame datatype.
  • Jon Skeet
    Jon Skeet about 13 years
    If that's what you meant, that's what you should have said. Given that this question is about the details of terminology, it's incredibly important to be precise.
  • nanofarad
    nanofarad almost 11 years
    This should really be a comment.
  • uranibaba
    uranibaba about 8 years
    I can't edit it because it is only one character. Please add a "y" to the quote in your answer. "The say that the[y] have to.."
  • SimonC
    SimonC about 2 years
    This didn't answer the question. When answering questions, make sure you read the question. This question has already been answered several times, the accepted answer is very high-quality. If you want to add an answer, make sure it contains information missing from the accepted answer.