What exactly is an instance in Java?
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
andreference
types.
Thereference
types areclass
types,interface
types, andarray
types.
There is also a specialnull
type.
An object is a dynamically created instance of aclass
type or a dynamically createdarray
.
The values of areference
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.
priya
Updated on July 09, 2022Comments
-
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?
-
jww over 9 yearspossible duplicate of Difference between object and instance
-
-
Voooza about 13 yearsI don't think that j is an object. It just stores reference to an object.
-
Jon Skeet about 13 yearsNote that
j
isn't even a reference, either. It's a variable. The value ofj
is a reference. It's very important to distinguish between variables and values, and objects and references. -
Voooza about 13 yearsI said stores reference not is reference. Maybe I don't express myself well, but thats what I meant.
-
Alpine about 13 years@Jon I had said
j
is the object that is created of the type JFrame. By that I meanj
is a variable of reference type which is aJFrame
datatype. -
Jon Skeet about 13 yearsIf 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 almost 11 yearsThis should really be a comment.
-
uranibaba about 8 yearsI 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 about 2 yearsThis 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.