[JAVA] Difference between class and instance

In this article, we aim to understand the nuances of classes, not the implementation or instantiation of them.

What is a class

Blueprint. Or something like a concept defined to create an entity.

What is an instance

An entity created based on a blueprint called a class. Take a car as an example We manufacture a ** instance ** (entity) called Benz from the ** class ** (design document) of the car. Each instance has different characteristics. If you make a black car, some of them will be Toyota cars, some will be Mercedes-Benz cars, they will be different in size and shape, and no instance will be the same.

Let's write the code and imagine

Here we use ** java **. We will create a car design document called a car class and create an instance.

1. Class design

First, let's create a car class design document. Please refer to the following page for the specific method of creating the design document. (Please wait as it is still under construction)

Car.java


class Car {
    //Field declaration
    
    public String name = ""; //Initial value of the name ・ ・ ・ 1

    public String color = ""; //Initial color value ・ ・ ・ 2

    public int speed = 0; //Initial value of speed ・ ・ ・ 3

    //Method declaration
    public void setCar(String n, Sring c) { //・ ・ ・ 4
        this.name = n; //Set the instance name ... 4-1
        this.color = c; //Set the instance color ... 4-2
    }

   public void setSpeed(int a) {// 5
        this.speed = a;//Initial value of speed ・ ・ ・ 5-1
    }
}

Even if you don't know java, you can understand it intuitively. I made a car design document (class). The defined contents are as follows

  1. Set the car to have a name feature
  2. Set the car to have the characteristic of color
  3. Set the car to have the characteristic of speed
  4. Add specifications that allow you to set the name and color of the car
  5. Set a name for the car instance
  6. Set the color for the car instance
  7. Add specifications that can set the speed to the car
  8. Set the speed on the car instance

This is just a design document, so unless you create an instance I just created a design document.

2. Create an instance

This time, create a car instance based on the created design document.

--The name is Benz --The color is white --Speed is 100

will do.

In the Test.Java class, as shown below Add a method called makeCar and write it in it.

Test.java



    public void makeCar() { 
       Car myCar = new Car(); //・ ・ ・ 1
       myCar.setCar("Benz","white"); //・ ・ ・ 2
       myCar.setSpeed(100); //・ ・ ・ 3
    }
}

The contents defined in this makeCar are

  1. Create a car instance
  2. Set the name Benz and the color white to the created instance.
  3. Set speed 100 for the created instance

Finally

When processing object-oriented in languages such as Java and objective-c, the words ** class ** and ** instance ** will appear repeatedly.

If you don't know, it's something you usually have around you, and you may find it easier to create a class or instance and replace it.

I wrote an article below that explains the difference between instance methods and class methods. I hope you can refer to that as well. (making)

If you have any suggestions, please let me know! !!

Recommended Posts

Difference between class and instance
Difference between instance method and class method
Difference between instance variable and class variable
Difference between variables and instance variables
[Java] Differences between instance variables and class variables
Difference between vh and%
Difference between i ++ and ++ i
Easy to understand the difference between Ruby instance method and class method.
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Relationship between package and class
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is the difference between a class and a struct? ?? ??
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
What is the difference between an action and an instance method?
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Difference between pop () and peek () in stack
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization