Java programming (class method)

Class method

Here, we will introduce class methods. Variables in a class that have static in their head at the time of declaration are called class variables. Similarly, a method that has static in its head when it is declared is called a class method. You can read it if you know the structure of the class and how to write and call the method. Also, the access specifier is omitted. (I think you can read it without knowing it.)

Features of class methods

Class methods do not instantiate when called. Use the class name when calling. There are some useful things and things to be aware of.

Class method configuration

I summarized how to write and use class methods.

How to define a class method

The way to write a class method is as follows.

public class class name{ 
public static return type method name(Argument type Argument){
  //The contents of the method
  }
}

It just has static. So there is no such thing as a configuration. However, static methods can only be used with static methods.

How to call a class method

Class method using class name without instance

name of the class.Method name(Argument type Argument);

Call like.

Class method example

Next, let's write an example of a class method. The class name is Call class.

public void Call(){
  public static void Dog(){
   System.out.println("Bow-wow");
  }
  
  public static void Cat(){
    System.out.println("Meow meow");
  }

  public static void Monkey(){
    System.out.println("Ukey");
  }
}

Call the static methods of this class from the main method. Note that we are calling using the class name instead of instantiating it.

public class MainMethod(){
 public static void main(String[] args){
  Call.Dog();
  Call.Cat();
  Call.Monkey();
 }
}

Now like this

Bow-wow
Meow meow
Ukey

Will be the output.

Class method notes

Class methods are not instantiated. In other words, it cannot be distinguished for each instance. For example, in the following class, the number of times the method in the class for each instance is called is counted by the variable instance_count. This variable instans_count makes sense because it is instantiated. So static methods don't make sense.

public class ClassValue2 {
    private int s = 3;
    private static int static_count = 0;
    private int instans_count = 0;

    public void add(int num){
        s = num + s;
        static_count = static_count + 1;
        instans_count = instans_count + 1;
    }

    public void substract(int num){
        s = -num + s;
        static_count = static_count + 1;
        instans_count = instans_count + 1;
    }

    public void multiple(int num){
        s = num*s;
        static_count = static_count + 1;
        instans_count = instans_count + 1;
    }
    public void division(int num){
        s = 1/num*s;
        static_count = static_count + 1;
        instans_count = instans_count + 1;
    }

    public void confirmation(){
        System.out.println("static_count:" + static_count);
        System.out.println("instans_count:" + instans_count);
        System.out.println("s:" + s);
    }

}

Recommended Posts

Java programming (class method)
Java programming (class structure)
Java method
java (method)
Java method
Class method
[Java] method
[Java] method
[Java] Object-oriented syntax --class method / argument
[Java] Class inheritance
java Scanner class
Java HashMap class
java programming basics
Java8 method reference
Java anonymous class
[Java] forEach method
java Generic Programming
About Java class
[java] abstract class
java8 method reference
[Java] Object class
Java local class
[Java] Random method
[Java] split method
[Beginner] Java method / class / external library [Note 23]
Java programming (static clauses and "class variables")
[Java] Instance method, instance field, class method, class field, constructor summary
About class division (Java)
About Java StringBuilder class
Java learning 2 (learning calculation method)
[Java] About Singleton Class
Java method call from RPG (method call in own class)
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 SE 11 Silver] Arrays class method summary [Java beginner]
Java programming basics practice-array
Java class type field
About Java String class
6th class: Variables, method
All about Java programming
java competitive programming memo
[Java] Basic method notes
About java abstract class
Java Programming Thread Runnable
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
[Java] Integer wrapper class reference
Java memo (standard class) substring
[Java] New Thread generation method (2)
Java tips --StaticUtility class modifier
Amazing Java programming (let's stop)
Java inflexible String class substring
Java GC method determination conditions
Java memo (standard class) length
Java programming (variables and data)
Java Silver Study Method Memo
[Implementation] Java Process class notes
About Java class loader types