What Exactly Is an Instance in Java

What exactly is an instance in Java?

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.

what is meaning of instance in programming?

"instance" is best understood as it relates to "class" in programming. "Classes" are used to define the properties and behavior of a category of things. E.g. A "Car" class might dictate that all cars be defined by their make, model, year, and mileage.

But you can't provide specifics about a particular car (for example, that 1978 Chevy Impala with 205,000 miles on it that your uncle Mickey drives) until you create an "instance" of a Car. It's the instance that captures the detailed information about one particular Car.

The difference between Classes, Objects, and Instances

Java (and any other programming language) is modeled 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 an instance of a reference type.

An object is a class instance or an array. (JLS 4.3.1)

That's the type theoretic view.

In practice, most Java developers treat the words "instance" and "object" as synonyms. (And that includes me then I'm trying to explain something quickly.) And most developers use the word "value" rather than "instance" to refer to an instance of a primitive type.

What is an instance variable in Java?

Instance variable is the variable declared inside a class, but outside a method: something like:

class IronMan {

/** These are all instance variables **/
public String realName;
public String[] superPowers;
public int age;

/** Getters and setters here **/
}

Now this IronMan Class can be instantiated in another class to use these variables. Something like:

class Avengers {

public static void main(String[] a) {
IronMan ironman = new IronMan();
ironman.realName = "Tony Stark";
// or
ironman.setAge(30);
}

}

This is how we use the instance variables. Shameless plug: This example was pulled from this free e-book here here.

Instance A = Instance B, what exactly does this do?

course1 is a variable, it reference an object, let's say object1.

course2 is another variable, it reference another object, let's say object2.

When you call course2 = course1, you make them reference the same object --object1.

What do we mean when we say instance?

Is obj variable refers to instance of A or B?

Both!

If you own a cat, do you own a cat or an animal? -- You can rightfully say you own both!

Paraphrased in code:

Animal a = new Cat();

a is a variable that holds a reference to a Cat object (which at the same time is an Animal object).

So...

...does a hold a reference to a Cat? Yes.

...does a hold a reference to an Animal? Yes.



Related Topics



Leave a reply



Submit