The difference between classes, objects, and instances

What is a class, object, and instance in Java?

+73
java object class instance
Aug 01 '09 at 5:08
source share
16 answers

Java (and any other programming language) is modeled in terms of types and values. At the theoretical level, a value is a representation of a certain amount of information, and a type is a collection of values. When we say that the value of X is an instance of type Y, we simply say that X is a member of the set of values, which is type Y.

So what the term “instance” really means: it describes a relationship, not a thing.

The Java programming language type system supports two types of types, primitive types and reference types. Link types are further divided into classes and array types. A Java object is an instance of a reference type.

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

This is a type of theoretical representation.

In practice, most Java developers interpret the words "instance" and "object" as synonyms. (And it concerns me, then I try to explain something quickly.) And most developers use the word "value" rather than "instance" to refer to an instance of a primitive type.

+75
Aug 01 '09 at 14:07
source share

A class is a project that you use to create objects . An object is an instance of a class - it is a specific thing that you created using a particular class. So, “object” and “instance” are one and the same, but the word “instance” indicates the relation of an object to its class.

This is easy to understand if you look at an example. For example, suppose you have a House class. Your own home is an object and is an instance of the House class. Your home is another object (another instance of the House class).

 // Class House describes what a house is class House { // ... } // You can use class House to create objects (instances of class House) House myHouse = new House(); House sistersHouse = new House(); 

The House class describes the concept of what a house is, and there are concrete, concrete houses that are objects and instances of the House class.

Note. This is the same in Java as in all object-oriented programming languages.

+86
Sep 28 '09 at 9:46
source share

A class is basically a definition and contains object code. An object is an instance of a class.

for example if you say

 String word = new String(); 

a class is a String class that describes the word object (instance).

When a class is declared, no memory is allocated, so the class is just a template.

When a class object is declared, memory is allocated.

+29
Aug 01 '09 at 5:14
source share

I like the layman’s explanation of Jesper

Improvising examples from Jesper's answer,

 class House { // blue print for House Objects } class Car { // blue print for Instances of Class Car } House myHouse = new House(); Car myCar = new Car(); 

myHouse and myCar are objects

myHouse is an instance of House (associates Object-myHouse with its Class-House). myCar is an instance of Car.

in short

"myHouse is an instance of the House class, " which means "myHouse is an object of type House."

+11
Oct 20 '13 at 20:12
source share

Class is a data type. This type is used to create an object.

  • An instance is logical, but an object is a physical tool that takes up some memory.

  • We can create an instance for an abstract class, as well as for an interface, but we cannot create a file object for them.

  • An object is an instance of a class and an instance representing an object of class ie

  • An instance refers to an object reference.

  • The object actually points to the memory address of this instance.

  • You cannot pass an instance over layers, but you can pass an object over layers

  • You cannot save an instance, but you can save an object

  • One object can have more than one instance.

  • Instances will have both a class definition and an object definition, where, as in an object, it will have only an object definition.

Object syntax:

  Classname var=new Classname(); 

But, for example, the creation returns only a pointer that references the object, syntax:

  Classname varname; 
+6
Apr 12 '13 at 12:14
source share

In java, objects appear in a heap of memory. This requires that the link be specified and used in our application. A link has an object memory location with which we can use objects in our application. A short reference is nothing more than the name of a variable that stores the address of an object created by an instance in a memory location.

An instance is a generic term for object . FYI, object is a class.

For example,

 Class A{ } A ref = new A(); 

For the above code snippet, ref is a reference for an object of class A generated on the heap.

+3
Jul 09 '13 at 7:09
source share

If you have a program that models cars, you have a class for representing cars, so in the code you can say:

 Car someCar = new Car(); 

someCar is now an instance of the Car class. If the program is used in a repair shop, and someCar represents your car in its system, then your car is an object.

So, a car is a class that any car in the real world can represent someCar - this is an example of the Car class and someCare is one real life object (your car)

however, instance and object are very often used interchangeably when it comes to discussing coding

+2
Aug 01 '09 at 14:17
source share

Any data stored on your computer and processes in the most basic representation is a string of bits. How these bits are interpreted is done through data types. Data types can be primitive or complex. Primitive data types - for example, int or double. They have a specific length and a specific way of interpretation. In the case of an integer, the first bit is usually used for the sign, the rest are used for the value.

Complex data types can be combinations of primitive and other complex data types and are called "Class" in Java.

You can define a complex PeopleName data type consisting of two lines called first name and last name. Each line in Java is another complex data type. Strings in the reverse order (possibly) are implemented using the primitive char data type, for which Java knows how many bits they take for storage and how to interpret them.

When you create an instance of a data type, you get an object, and your computers reserve some memory for it and remember its location and the name of that instance. The PeopleName instance in memory will occupy the space of two String variables plus a little more for bookkeeping. An integer takes 32 bits in Java.

Complex data types may have methods assigned to them. Methods can take actions on their arguments or on an instance of the data type called by this method. If you have two instances of PeopleName called p1 and p2, and you call p1.getFirstName (), it usually returns the first name of the first person, but not the second person.

+2
Sep 28 '09 at 9:39
source share

The concept of classes and objects is to encapsulate logic in a single programming unit. Classes are drawings from which objects are created.

Here is an example of a class representing Car:

 public class Car { int currentSpeed; String name; public void accelerate() { } public void park() { } public void printCurrentSpeed() { } } 

You can create instances for an object . For example:

 Car audi = new Car(); Car toyota = new Car(); 

I gave an example from this lesson.

+2
Nov 15 '14 at 20:34
source share

Honestly, I feel more comfortable with Alfred’s blog definitions:

Object : real-world objects have two main characteristics: state and behavior. A person has a state (name, age) and behavior (running, sleeping). The car has a state (current speed, current gear) and behavior (applying the brake, shifting gears). Software objects are conceptually similar to real-world objects: they also consist of state and associated behavior. The object saves its state in the fields and reveals its behavior using methods.

Class : This is the "template" / "plan" that is used to create objects. In essence, the class will consist of a field, a static field, a method, a static method, and a constructor. The field is used to store the state of the class (for example: the name of the Student object). The method is used to represent class behavior (for example, how a student’s object is about to stand up). The constructor is used to create a new instance of the class.

Instance . An instance is a unique copy of a class that represents an object. When a new instance of the class is created, the JVM allocates memory space for this instance of the class.

Given the following example:

 public class Person { private int id; private String name; private int age; public Person (int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (id != other.id) return false; return true; } public static void main(String[] args) { //case 1 Person p1 = new Person(1, "Carlos", 20); Person p2 = new Person(1, "Carlos", 20); //case 2 Person p3 = new Person(2, "John", 15); Person p4 = new Person(3, "Mary", 17); } } 

For case 1, there are two instances of the Person class, but both instances represent the same object.

For case 2, there are two instances of the Person class, but each instance represents a separate object.

So a class, an object, and an instance are two different things. An object and an instance are not synonyms, as suggested in the answer selected as the correct answer.

+2
Jan 31 '18 at 16:42
source share

Class

  • It has a logical existence, that is, when it is created, the memory space is not allocated.

  • This is a collection of objects.

  • A class can be considered as a project for creating objects.

    • Created Using Keyword

    • The class defines the methods and data elements that the objects will possess.




An object

  • He has a physical existence, i.e. memory space is allocated when it is created.

  • This is an instance of the class.

  • An object is a unique object that contains data elements and member functions together in the OOP language.

    • Created using a new keyword

    • An object defines implementations of methods and values ​​that will belong to data members in a class.

+1
May 20 '16 at 12:44
source share

Class : Structure

Object : Physical Manifestation

Instance : each object created from a class

Link : object address

+1
Jun 13 '19 at 9:12
source share

A class is the plan needed to create an object (= instance).

The difference between an object and an instance is that an object is a thing, and an instance is a relation.

In other words, instance describes the relationship of the object to the class from which the object was created.

0
Mar 28 '19 at 5:30
source share

The definition "An object is an instance of a class" is conceptually incorrect, but true according to the implementation. In fact, object-oriented functions are taken from real life, in order to focus the programmer’s mind from larger to smaller. In real life, classes are designed to control an object. For example, we have caste, religion, nationality and much more. These castes, religion, nationality are classes and cannot exist without people. But in the implementation there is no existence of objects without classes. Object-Object is a discrete object that has some well-defined attribute. Here, discrete means something that makes it unique from another. A well-defined attribute makes sense in some context. Class - classification of an object having some common behavior or objects of some general type.

-one
Jan 10 '16 at 6:35
source share

While the answers above are correct, another way of thinking about classes and objects would be to use real-world examples: a class named Animal may contain objects such as Cat, Dog, or Fish. An object with the name of the Bible will have a class book, etc. Classes are common, objects are specific. This mental example helped me when I was learning Java.

-one
Mar 02 '16 at 21:35
source share

A class is a template or type. An object is an instance of a class.

For example:

 public class Tweet { } Tweet newTweet = new Tweet(); 

Tweet is the class, and newTweet is the class object.

-2
Sep 04 '16 at 9:48
source share



All Articles