What is the difference between instance and object in java




















When we use class UserData to represent the user of the program, there can only be one user, since we only have memory space to store data about one user. Note that the class, UserData , and the variables it contains exist as long as the program runs. That is essentially what it means to be "static. I've also included a static variable in the PlayerData class. Here, the static variable playerCount is stored as part of the representation of the class in memory.

Its full name is PlayerData. However, the other two variables in the class definition are non-static. There is no such variable as PlayerData. But the PlayerData class can be used to create objects. There can be many objects created using the class, and each one will have its own variables called name and age. This is what it means for the non-static parts of the class to be a template for objects: Every object gets its own copy of the non-static part of the class.

We can visualize the situation in the computer's memory after several objects have been created like this:. Note that the static variable playerCount is part of the class, and there is only one copy. On the other hand, every object contains a name and an age. An object that is created from a class is called an instance of that class, and as the picture shows, every object "knows" which class was used to create it.

I've shown class PlayerData as containing something called a "constructor;" the constructor is a subroutine that creates objects. Now there can be many "players," because we can make new objects to represent new players on demand. A program might use the PlayerData class to store information about multiple players in a game.

Each player has a name and an age. When a player joins the game, a new PlayerData object can be created to represent that player. If a player leaves the game, the PlayerData object that represents that player can be destroyed.

A system of objects in the program is being used to dynamically model what is happening in the game. You can't do this with static variables! An object that is created using a class is said to be an instance of that class. We will sometimes say that the object belongs to the class. The variables that the object contains are called instance variables. The methods that is, subroutines that the object contains are called instance methods.

For example, if the PlayerData class, as defined above, is used to create an object, then that object is an instance of the PlayerData class, and name and age are instance variables in the object. My examples here don't include any methods, but methods work similarly to variables. Static methods are part of the class; non-static, or instance, methods become part of objects created from the class. It's not literally true that each object contains its own copy of the actual compiled code for an instance method.

But logically an instance method is part of the object, and I will continue to say that the object "contains" the instance method. Note that you should distinguish between the source code for the class, and the class itself in memory. The source code determines both the class and the objects that are created from that class. The "static" definitions in the source code specify the things that are part of the class itself in the computer's memory , whereas the non-static definitions in the source code specify things that will become part of every instance object that is created from the class.

By the way, static member variables and static member subroutines in a class are sometimes called class variables and class methods , since they belong to the class itself, rather than to instances of that class. As you can see, the static and the non-static portions of a class are very different things and serve very different purposes. Many classes contain only static members, or only non-static, and we will see only a few examples of classes that contain a mixture of the two.

So far, I've been talking mostly in generalities, and I haven't given you much of an idea about what you have to put in a program if you want to work with objects. Let's look at a specific example to see how it works.

Consider this extremely simplified version of a Student class, which could be used to store information about students taking a course:. None of the members of this class are declared to be static , so the class exists only for creating objects. This class definition says that any object that is an instance of the Student class will include instance variables named name , test1 , test2 , and test3 , and it will include an instance method named getAverage.

The names and test grades in different objects will generally have different values. When called for a particular student, the method getAverage will compute an average using that student's test grades.

Different students can have different averages. Again, this is what it means to say that an instance method belongs to an individual object, not to the class.

In Java, a class is a type , similar to the built-in types such as int and boolean. So, a class name can be used to specify the type of a variable in a declaration statement, or the type of a formal parameter, or the return type of a function. For example, a program could define a variable named std of type Student with the statement. However, declaring a variable does not create an object!

This is an important point, which is related to this Very Important Fact:. In Java, no variable can ever hold an object. A variable can only hold a reference to an object. You should think of objects as floating around independently in the computer's memory. In fact, there is a special portion of memory called the heap where objects live. Instead of holding an object itself, a variable holds the information necessary to find the object in memory.

This information is called a reference or pointer to the object. In effect, a reference to an object is the address of the memory location where the object is stored. When you use a variable of object type, the computer uses the reference in the variable to find the actual object. In a program, objects are created using an operator called new , which creates an object and returns a reference to that object. In fact, the new operator calls a special subroutine called a "constructor" in the class.

For example, assuming that std is a variable of type Student , declared as above, the assignment statement. The value of the variable is a reference, or pointer, to the object. The object itself is somewhere in the heap. It is not quite true, then, to say that the object is the "value of the variable std " though sometimes it is hard to avoid using this terminology. It is certainly not at all true to say that the object is "stored in the variable std. If I ever say something like "std is an object," you should read it as meaning "std is a variable that refers to an object.

So, suppose that the variable std refers to an object that is an instance of class Student. That object contains instance variables name , test1 , test2 , and test3.

These instance variables can be referred to as std. This follows the usual naming convention that when B is part of A , then the full name of B is A. For example, a program might include the lines. This would output the name and test grades from the object to which std refers.

Similarly, std can be used to call the getAverage instance method in the object by saying std. To print out the student's average, you could say:. More generally, you could use std. You can use it in expressions. You can assign a value to it. You can even use it to call subroutines from the String class. For example, std. A single object can have more than one instance. You are commenting using your WordPress.

You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email.

This site uses Akismet to reduce spam. Learn how your comment data is processed. Search Search for:. Like this: Like Loading Now, Java objects are similar to real-world objects.

For example we can create a car object in Java, which will have properties like current speed and color; and behavior like: accelerate and park. Check it out to learn more about Java classes and object. When a variable is declared of a custom type class , only a reference is created, which is called an object. At this stage, no memory is allocated to this object. It acts just as a pointer to the location where the object will be stored in future. This process is called 'Declaration'.

On the other hand, when a variable of custom type is declared using the new operator, which allocates memory in heap to this object and returns the reference to the allocated memory. This object which is now termed as instance. This process is called 'Instantiation'. However, in some languages such as Java, an object is equivalent to an instance, as evident from the line written in Oracle's documentation on Java :.

Note: The phrase "instantiating a class" means the same thing as "creating an object. Class : A class is a blue print. Object : It is the copy of the class. Instance : Its a variable which is used to hold memory address of the object. But you can't live in the blue print. You need a physical House which is the instance of the class to live in. Instances represent objects. Object : When memory is allocated to the data entity created from blueprint class , that data entity or reference to it is called Object.

Instance : When data is filled in an Object , it becomes an instance of that Object. It can also be called a state of that Object. That Object is somewhere on your RAM. And the reference to that Object is in "a". We have 1 object and 1 instance at this time.

Now in next line, we assign b with a. Here Object is not copied but the reference of object from "a" is stored in "b" too. Thus , we have 2 instances , "a and b". Finally , we have 4 instances "a,b,c,d" of a single object that was created with new Operator. I can't believe this could be hard to be explain but it actually easier than all the answers I read. It just simple like this. Regarding the difference between an object and an instance , I do not think there is any consensus.

It looks to me like people change it pretty much interchangeably, in papers, blog posts, books or conversations. As for me, the way I see it is, an object is a generic and alive entity in the memory, specified by the language it is used in.

Just like the Object class in Java. We do not much care its type, or anything else associated with it, whether it is managed by a container or not. An instance is an object but associated with a type, as in this method accepts Foo instances , or you can not put Animal instances in an instance of a List of Vehicles. Then you run your application. You now have 2 instances of the object basketball. Object refers to class and instance refers to an object.

In other words instance is a copy of an object with particular values in it. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the difference between an Instance and an Object? Ask Question. Asked 11 years, 5 months ago. Active 2 months ago. Viewed k times. Is there a difference or not? Improve this question. Tim S. Van Haren 8, 2 2 gold badges 28 28 silver badges 34 34 bronze badges. Maybe you can deduce from the well known error message "Object reference not set to an instance of an object.

From JVM spec: "An object is either a dynamically allocated class instance or an array. StackOverFlow I have just given a brief description on difference between object and instance I hope it helps — Pushkarraj Pujari.

Add a comment. Active Oldest Votes. Improve this answer. Smart Manoj 4, 3 3 gold badges 27 27 silver badges 46 46 bronze badges. What about languages that do not have classes but do have structs? Could I say "an instance of a struct" or would that be intrinsically wrong?

How should I call it in that case? Excellent question.



0コメント

  • 1000 / 1000