A class is used to bind data as well as methods together as a single unit. Object acts like a variable of the class. Classes have logical existence. Objects have a physical existence.
A class doesn't take any memory spaces when a programmer creates one. The non-static parts of the class specify, or describe, what variables and methods the objects will contain. This is part of the explanation of how objects differ from classes: Objects are created and destroyed as the program runs, and there can be many objects with the same structure, if they are created using the same class.
Consider a simple class whose job is to group together a few static member variables. For example, the following class could be used to store information about the person who is using the program:. In a program that uses this class, there is only one copy of each of the variables UserData. When the class is loaded into the computer, there is a section of memory devoted to the class, and that section of memory includes space for the values of the variables name and age.
We can picture the class in memory as looking like this:. An important point is that the static member variables are part of the representation of the class in memory.
Their full names, UserData. 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. Object-oriented programming languages have two different essential concepts, namely objects and classes.
An object is an instantiation of a class. The difference is very conceptual, even though some people use them interchangeably. The difference between objects and classes is that objects are an instance of a class, whereas a class acts as a blueprint that can be used to created instances like objects. Classes work as a sort of template for an object, and they can also describe object behavior. An object can be defined as a physical entity that is used in using commands of a programming language.
It is an instance of a class and can be a variable, data structure, function, or value. It mainly consists of two things, data also known as a state and code also known as behavior. Each object is responsible for carrying out its own tasks in programming. On the other hand, a class acts as a blueprint of an object. It is a form of concept that can be used in object-oriented programming languages. It represents every information and all the methods that an object should have.
It also helps in providing values for member variables state and implementations of behavior in various programs.
0コメント