[Java] Instance method, instance field, class method, class field, constructor summary

Summarize some of the confusing parts of learning Java. I'm referring to Progate.

1. Instance method

The instance method is the "behavior" that the instance has and the instance has.

How to define a method

public <Return type> <Method name>(){
 <The contents of the method>
}

Write like.

How to call a method


/*Create an instance of Person class, person1*/
Person person1 = new Person();

/*Call the hello method*/
person1.hello();

Call like.

2. Instance field

The instance field is a variable that stores the information that the instance has.

How to define instance fields

class Person {
  /*Declare the type because it is a variable*/
  public String name;
}

How to access instance fields

/*Create an instance*/
Person person1 = new Person();
/*Set a value for name*/
person1.name = "Suzuki";

/*Get the value of name*/
System.out.println(person1.name);

You can access it like this.

3. Class method

A method that belongs to a class.

How to define a class method

class Person {
  public static <Return type> <Method name>() {
    <The contents of the method>
  }
}

How to call a class method

<name of the class>.<Method name>();

Call it like this. The public static void <method> () {} seen from the parent's face is actually a class method.

4. Class field

Fields that belong to the class. A variable that stores the information that the class has.

How to define class fields

class Person {
  public static <Data type> <Variable name>;
}

How to access the class field

Main.java


class Main {
  public static void main(String[] args) {
    System.out.println("total" + Person.count + "Is a person");
    Person person1 = new Person( ... );
    System.out.println("total" + Person.count + "Is a person");
  }
}

Person.java


class Person {
  /*Store information about the number of people in a variable called count*/
  public static int count = 0;
  .........
  Person(String firstName, ...) {
    Person.count ++;
  }
}

python


>0 people in total
>1 person in total

5. Constructor

A method that is automatically called after creating an instance with new. Note that the definition method is fixed

How to define a constructor

python


class Person {
  public String name;
  Person() {
    /*What you want to do when creating an instance*/
  }
}

The constructor is the part of Person () {}. It is similar in writing and declaring a method. However, unlike the method, there is no return value and no void is required. Because the return value of the constructor is always an instance of that class, The compiler will tell you even if you don't specify it. Below is an example of a compiler.

When creating an instance, pass the value you want to set in the instance field to the argument of the constructor

Person.java


class Person {
  public String name;
  Person(String name) {
    this.name = name;
  }
}

How to call the constructor

Main.java


Person person = new Person("Suzuki");
System.out.println(person.name);

6. Summary

Modifier static Data type Method name or field variable name Method name
Instance method public - Data type Method name
Instance field public - Data type Variable name of the field
Class method public static Data type Method name
Class field public static Data type Variable name of the field
constructor public - Data type Variable name name of the class

It's easy to get confused! that's all!

Recommended Posts

[Java] Instance method, instance field, class method, class field, constructor summary
Java class type field
Java programming (class method)
[Java SE 11 Silver] Arrays class method summary [Java beginner]
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
Instance creation, constructor, field, etc.
Summary of Java Math class
Difference between instance method and class method
[For beginners] Summary of java constructor
[Java] Object-oriented syntax --class method / argument
java (use class type for field)
Java method
java (constructor)
java (method)
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
Class method
Java constructor
[Java] method
[Java] method
[Beginner] Java method / class / external library [Note 23]
[Java] Differences between instance variables and class variables
Java class methods
Java knowledge summary
[Java] Class inheritance
Java Generics Summary
java Scanner class
Java method call from RPG (method call in own class)
java (abstract class)
Java8 method reference
[Java] Exception instance
[Java] Nested class
Java anonymous class
[Java] forEach method
Java 8 documentation summary
About Java class
Java 11 document summary
[java] abstract class
java8 method reference
Java local class
[Java] Random method
Understand java constructor
[Java] split method
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
[Java] How to use compareTo method of Date class
Summary of file reading method for each Java file format
[Java Silver] What are class variables instance variables and local variables?
Java 12 new feature summary
About class division (Java)
JAVA DB connection method
[Summary] Java environment preparation
effective java 3rd summary
Java learning 2 (learning calculation method)
Java 13 new feature summary
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Java inner class review
Java static [Personal summary]
Studying Java 8 (see constructor)