[Java] Various summaries attached to the heads of classes and members

Static member

Just put static in the head. The example below is an image of multiple people holding one wallet. In this case, the idea is that each Human instance h1, h2 does not have a field, but the Human class has a field. So static fields are also called ** class variables **.


public class Human{
     static int money=100;


public class Main{
     public static void main(String[] args){
          //Create the first instance
          Human h1 = new Human();
          //Create a second instance
          Human h2 = new Human();

          // h1.Rewrite money class variable

          // h2.Show money variable
          //Since money is a class variable, it should be displayed as 200

Static methods can be called without instantiating. So, in the above example, suddenly



It's okay to write something like that. That's why the main method is a static method.


One way to achieve encapsulation is to add access modifiers to classes and members to set visibility.

--public: All classes --protected: Classes that belong to the same package as you or inherit from you --packaged private (default): Classes that belong to the same package as you --private: Only your own class


public class Test{
     private int i=1;
     public void calc(int j){
          return this.i+j;

getter and setter

The basic way to write how to set the field to private and the method to public and pass the value with getter and setter.


public class Test{
    private int num;

    public int getNum(){
        return this.num;

    public void setNum(int num){
        this.num = num;

Inheritance / implementation

--Inheritance: ʻexetends --Implementation: ʻimplements

Inheritance (exetends)

Here, the inheritance relationship is ʻInuShibainu. Note that the ʻosuwari method is abstracted and needs to be overridden by the Shibainu class.


public abstract class Inu{
    public void ote(){
        System.out.println("Hold out your left hand");
    public abstract void osuwari();
        //I don't write anything here


public class Shibainu extends Inu{
    public void osuwari(){
        System.out.println("Sit on the ground");

    public void mate(){
        System.out.println("Put up with rice");


public class Main{
    public static void main(Strgin[] args){
        Shibainu dog = new Shibainu();

        dog.ote();      //Inu derived method
        dog.osuwari();  // Inu(←Shibainu)Origin method
        dog.mate();     //Shiba inu derived method

Using ** polymorphism **


Inu dog = new Shibainu();


You can also say that.

In this case, the only methods that can be called are those that the ʻInuclass has. Therefore, even if the actually created instance isShibainu, dog.mate cannot be called. When the ʻosuwari method is called, the method of Shibainu is processed instead of the method of ʻInu`.

Using this polymorphism, the following processing can also be written.


Inu[] dogs = new Inu[3];

Inu[0] = new Shibainu();
Inu[1] = new Akitaken();
Inu[2] = new Tosaken();

for (Inu d: dogs){

It is very convenient when the contents of the same mate method, such as" sit on the ground "," sleep "," do not listen ", etc., differ depending on the class.


There are two conditions that can make a class an interface.

-** All methods are abstract methods. ** ** -** Basically it doesn't have any fields. ** **

"Basically" is because only fields (constants) with public static final are allowed to be declared. However, in that case, public static final may be omitted.

Declare ʻinterface instead of class`.


public interface Animal{
    void run();


public class Neko implements Animal{
    void run(){
        System.out.println("Run on four legs");

Since the interface allows multiple implementations


public calss Neko extends Mammal implements Animal, Organic{
    //It has Mammal class, Animal interface and Organic interface as parents.

You can also write like this. The cat class has a mammal class as a parent and has an interface between animals and organic matter. It's not a very good example.

The beauty of interfaces and abstract methods is

-** Forced to override. ** ** -** Avoid unintended instantiation. ** ** -** Clearly distinguishable from methods that do nothing. ** **

Such a place.

Reference book

[Introduction to Java 2nd Edition] (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) Pp.332-524

Recommended Posts

[Java] Various summaries attached to the heads of classes and members
Java classes and instances to understand in the figure
I tried to summarize the basics of kotlin and java
Command to check the number and status of Java threads
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
Output of the book "Introduction to Java"
The story of forgetting to close a file in Java and failing
[Java improvement case] How to reach the limit of self-study and beyond
I translated the grammar of R and Java [Updated from time to time]
About the idea of anonymous classes in Java
[Java] Personal summary of classes and methods (basic)
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
JAVA: Realizes generation and scanning of various barcodes
[Java] How to get the authority of the folder
Java Welcome to the Swamp of 2D Arrays
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
[Java] How to get the URL of the transition source
Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
Please note the division (division) of java kotlin Int and Int
The comparison of enums is ==, and equals is good [Java]
A memo about the types of Java O/R mappers and how to select them
Convert the array of errors.full_messages to characters and output
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[Java] How to get the maximum value of HashMap
I summarized the types and basics of Java exceptions
Java: Use Stream to sort the contents of the collection
Equivalence comparison of Java wrapper classes and primitive types
java (classes and instances)
Setting method to link Java of Eclipse and Github / September 2017
[For beginners] Explanation of classes, instances, and statics in Java
[Note] Java Output of the sum of odd and even elements
Generate and execute Jar file of Java file belonging to package
I didn't understand the behavior of Java Scanner and .nextLine ().
Difference between Java and JavaScript (how to find the average)
Get to the abbreviations from 5 examples of iterating Java lists
20190803_Java & k8s on Azure The story of going to the festival
Java beginners briefly summarized the behavior of Array and ArrayList
[Ruby] Creating code using the concept of classes and instances
[Java] To know the type information of type parameters at runtime
How to derive the last day of the month in Java
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
I want to implement various functions with kotlin and java!
The story of pushing Java to Heroku using the BitBucket pipeline
How to check the extension and size of uploaded files
Be sure to compare the result of Java compareTo with 0
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
[Java] I thought about the merits and uses of "interface"
[Java] Generics classes and generics methods
[Java] Classes, constructors, static members
Advantages and disadvantages of Java
Java abstract methods and classes
Input to the Java console
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.
[Rails] How to get the URL of the transition source and redirect
[Java] Get the dates of the past Monday and Sunday in order
[Swift5] How to get an array and the complement of arrays