What is a class in Java language (2 /?)

at first

Good evening. Again. Thank you for reading and commenting on many articles the other day. I was honestly surprised because I didn't expect to see it so far. And I'm sorry I posted an article with childish content. I will get used to it from now on, so I hope you can put up with it a little more and get along with me.

instance

This time I will write about instances. As I received your comment, the program for making cats is different from the original program because object-oriented programming is for writing instructions, but this time I will use cats for the sake of clarity. Also, like the other day, I will write the text I used to study java in the references, so please read it. Now, let's write a program to make many cats as an example.

Cat.java


public class Cat{
  String name;
  int onaka;
  static int esa = 10;

  Cat(){
  }

  Cat(String name){
    this.name = name;
  }

  void run(){
    System.out.printlf(name + "Ran");
    name.onaka -= 10;
  }

  void gohan(int eat){
    this.esa -= eat;
    this.onaka += eat;
  }
}

The cat has a name and is hungry. I'm hungry when I run. Also, since cats eat rice, we defined the remaining amount of rice in the house. Eating rice will satisfy your hunger. Also, Cat () and Cat (String name) are called constructors, which are called when an instance of Cat is created. Instances are described below. Therefore, when instantiating Cat, Cat () that does nothing if there is no argument is called, and Cat (String name) that sets the name if there is a String type argument is called. Creating methods with different arguments and type order, even if they are the same method, is called overloading.

Main.java


public class Main{
  public static void main(String[] args){
    Cat a = new Cat();
    a.name = "kuro";
    a.onaka = 50;
    Cat b = new Cat("shiro");
    b.onaka = 30;

    a.run();
    a.gohan(5);
  }
}

I will explain this Main.java. The right side of the third line creates an instance of the Cat class. By using an instance, you can create two types of cats with the following description, and each can have a value. The instance created here is assigned to the Cat type variable a on the left side. In the two lines below that, the cat in a is named kuro and the hunger is set. Next, on the left side of the 6th line, shiro is included in the value to be passed. This calls Cat (Sting name) on line 9 of the Cat class, where we set the name of cat b. After that, the hunger level of cat b is set. The next line calls the run () method to run cat a, which reduces the hunger of cat a by 10 as described in the run () method of the Cat class. After that, cat a is eating 5 rice. This is the point of this time, but the esa field of the Cat class has something called static. If static is attached to the field, it means that the value is shared by all instances, so 95 is displayed regardless of whether cat a or cat b outputs the remaining amount of esa. static is convenient! !! !! !! !! That's all for today

At the end

I was able to put together an article in a relatively short time today. No way I could write so quickly in yesterday's day ... I don't have any plans tomorrow, so I'll study Vue.js in the morning and write an article in the afternoon. Tomorrow I will probably write about encapsulation. Thank you for reading this far. I would appreciate it if you could comment if there are any mistakes or strange expressions.

References

[Introduction to Java 2nd Edition (Refreshing Series)](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA % E3% 82% 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9 % E3% 83% 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8% AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X / ref = zg_bs_515820_1? _Encoding = UTF8 & psc = 1 & refRID = JGCC33P1VGV3V5VKQGQR)

Recommended Posts

What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
What is a wrapper class?
What is a Java collection?
[Java] What is class inheritance?
[Java basics] What is Class?
What is a snippet in programming?
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
What is java
What is Java <>?
What is Java
What is the main method in Java?
Creating a matrix class in Java Part 1
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
What is a Servlet?
[Java] What is JavaBeans?
[Java] What is ArrayList?
GetInstance () from a @Singleton class in Groovy from Java
A quick review of Java learned in class
What is Java Assertion? Summary.
What is a Ruby module?
What is the difference between a class and a struct? ?? ??
What is a floating point?
A quick review of Java learned in class part4
What is a meaningful comment?
Write a class that can be ordered in Java
Write a class in Kotlin and call it in Java
What is the BufferedReader class?
What is a jar file?
Find a subset in Java
What is a lambda expression?
What I learned when building a server in Java
[Java] What is jaee j2ee?
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Cause of is not visible when calling a method of another class in java
What is a fa⁉ enum?
Java11: Run Java code in a single file as is
What is java escape analysis?
What is the LocalDateTime class? [Java beginner] -Date and time class-
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
[Java] A class is an OS, and an instance is a virtual computer.
Road to Java Engineer Part2 What kind of language is Java?
What is JVM (Java Virtual Machine)?
I created a PDF in Java.
How slow is a Java Scanner?
thread safe process in java language
StringBuffer and StringBuilder Class in Java
A simple sample callback in Java
[Java] What got caught in encapsulation
What is thread safe (with Java)
What is a column Boolean type?
What is a reference type variable?
What is @Autowired in Spring boot?
[Java] What is Concurrent Modification Exception?
Java Calendar is not a singleton.