Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is an instance in Java?

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?

like image 873
priya Avatar asked Feb 26 '11 09:02

priya


People also ask

What is instance in Java with example?

Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.

What is an instance in simple terms?

instance, case, illustration, example, sample, specimen mean something that exhibits distinguishing characteristics in its category. instance applies to any individual person, act, or thing that may be offered to illustrate or explain.

Why do we need instance in Java?

The variables that are declared inside the class but outside the scope of any method are called instance variables in Java. The instance variable is initialized at the time of the class loading or when an object of the class is created.

What is the purpose of instance?

An instance variable reserves memory for the data your class needs. Let's assume you want to add a place for a string or int variable. You can use an instance variable to reserve that memory for the lifetime of the object. Each object will receive unique memory for its variables.


Video Answer


1 Answers

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.

like image 169
Mark Byers Avatar answered Sep 23 '22 06:09

Mark Byers