What exactly is an instance in java?

What is the difference between an object, instance, & reference? They say that they have sầu to create an instance lớn their application? What does that mean?


*

*

An object và an instance are the same thing.

Bạn đang xem: What exactly is an instance in java?

Personally I prefer to use the word "instance" when referring lớn 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 lớn a specific object or else it can be a null reference.

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

They probably mean you have to lớn 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 và ask for a more complete example.


Share
Improve this answer
Follow
edited Mar 2 "16 at 17:26
*

DropArt
15511 gold badge11 silver badge1414 bronze badges
answered Feb 26 "11 at 9:25
*

Mark ByersMark Byers
711k160160 gold badges14931493 silver badges14071407 bronze badges
1
Add a bình luận |
12
"instance to lớn 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 tốc độ, horse-power, brand, etc.

Reference is, in the Java context, a variable* - it is something pointing to lớn 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 và 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 lớn distinguish between variables and values, và objects và references.


Share
Improve sầu this answer
Follow
edited Feb 26 "11 at 9:36
answered Feb 26 "11 at 9:25
*

BozhoBozho
550k135135 gold badges10221022 silver badges11141114 bronze badges
Add a comment |
9
When you use the keywords 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 và returning a reference to lớn 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.

Xem thêm: Mua Pin Samsung Chính Hãng Ở Đâu Tốt Nhất Ở Hà Nội, Tphcm? Cách Phân Biệt Pin Samsung Chính Hãng Qua 4 Bước

Take a look hereCreating 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, & 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, và Variables for more information


Share
Improve this answer
Follow
edited Feb 26 "11 at 9:42
answered Feb 26 "11 at 9:24
AlpineAlpine
3,70211 gold badge2323 silver badges1818 bronze badges
5
Add a comment |
8
I think that Object = Instance. Reference is a "link" lớn an Object.

Car c = new Car();variable c stores a reference to lớn an object of type Car.


Share
Improve this answer
Follow
answered Feb 26 "11 at 9:25
VooozaVoooza
73144 silver badges88 bronze badges
Add a comment |
5
The main differnece is when you say ClassName obj = null; you are just creating an object for that class. It"s not an instance of that class.

This statement will just allot memory for the static meber variables, not for the normal thành viên variables.

But when you say ClassName obj = new ClassName(); you are creating an instance of the class. This staement will allot memory all member variables.


Share
Improve sầu this answer
Follow
answered May 17 "12 at 5:35
KumarKumar
23722 silver badges99 bronze badges
Add a bình luận |
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.

Xem thêm: Mã Kích Hoạt Avast Free Antivirus 2018 Full Bản Quyền, Avast Premier 2018 Full Bản Quyền


Share
Improve this answer
Follow
edited Sep 24 "12 at 15:59
Code-Apprentice
68.4k1717 gold badges115115 silver badges223223 bronze badges
answered Sep 24 "12 at 15:26
vijayvijay
5111 silver badge11 bronze badge
Add a bình luận |
4
basically object & instance are the two words used interchangeably.A class is template for an object & an object is an instance of a class.


Share
Improve this answer
Follow
answered Jul 8 "15 at 6:54
karunesh palkarunesh pal
4111 bronze badge
Add a bình luận |
1
"creating an instance of a class" how about, "you are taking a class và making a new variable of that class that WILL change depending on an input that changes"

Class in the library called Nacho

variable Libre to lớn hold the "instance" that will change

Namang đến Libre = new Nacho(Variable, Scanner Input, or whatever goes here, This is the place that accepts the changes then puts the value in "Libre" on the left side of the equals sign (you know "Nacho Libre = new Nacho(Scanner.in)" "Namang lại Libre" is on the left of the = (that"s not tech talk, that"s my way of explaining it)

I think that is better than saying "instance of type" or "instance of class". Really the point is it just needs lớn be detailed out more.... "instance of type or class" is not good enough for the beginner..... wow, its like a tongue twister and your brain cannot focus on tongue twisters very well.... that "instance" word is very annoying và the mere sound of it drives me nuts.... it begs for more detail.....it begs to lớn be broken down better. I had to google what "instance" meant just to get my bearings straight..... try saying "instance of class" khổng lồ your grandma.... yikes!


Chuyên mục: Công Nghệ