Check static and public behavior in Java methods

Background

-Does the class itself have it? Disclosure range?

Caution

――Aside from "when to use" --All classes are fixed as public --Miscellaneous

Just a method

This is the simplest form Java has qualifiers in the main method, which makes it difficult to get started.

Calculator.java


package jbasic;

public class Calculator {
	 int add(int number1, int number2) {
		int result = number1 + number2;
		return result;
	}
}

When you can use just a method

An instance of the class in which the method is written has been created The class in which the method is written has not been instantiated
Same package ×
Package is different × ×

Seller.java


package jbasic; //Same package

public class Seller {
	public static void main(String[] args) {
		Calculator calc = new Calculator();  //Instance generation
		System.out.println(calc.add(10,5));  //OK!
	}
}

Seller2.java


package jbasic; //Same package

public class Seller2 {
	public static void main(String[] args) {
        //No instantiation
	    System.out.println(Calculator.add(10,5));  //Get angry at not statically referencing non-static methods
	}
}

static method

With this, you can hit the method without instantiating it.

Calculator.java


package jbasic;

public class Calculator {
	 static int add(int number1, int number2) {
		int result = number1 + number2;
		return result;
	}
}

Timing when static methods can be used

An instance of the class in which the method is written has been created The class in which the method is written has not been instantiated
Same package
Package is different × ×

Seller2.java


package jbasic; //Same package

public class Seller2 {
	public static void main(String[] args) {
        //No instantiation
		System.out.println(Calculator.add(10,5));  //OK!
	}
}

Robot.java


package jmaster; //Different package

public class Robot {
	public static void main(String[] args) {
		System.out.println(Calculator.add(10,5));  //Get angry to make the class public
	}
}

public static method

If you add public, you can use the method from the class of other packages.

Calculator.java


package jbasic;

public class Calculator {
	 public static int add(int number1, int number2) {
		int result = number1 + number2;
		return result;
	}
}

When to use public static methods

An instance of the class in which the method is written has been created The class in which the method is written has not been instantiated
Same package
Package is different

Robot.java


package jmaster; //Different package

import jbasic.Calculator; //Class import required

public class Robot {
	public static void main(String[] args) {
		System.out.println(Calculator.add(10,5));  //OK!
	}
}

Isn't it all public static?

--Static method cannot use instance field --If it is public, it may be used strangely and it may be a problem (I understand, but I do not understand the actual harmful effects)

Recommended Posts

Check static and public behavior in Java methods
About Java static and non-static methods
Define abstract methods in Java enum and write each behavior
[Java] Difference between static final and final in member variables
[Java] Stack area and static area
[Java] Generics classes and generics methods
Check https connection in Java
Java methods and method overloads
Java abstract methods and classes
Mock static methods in Mockito 3.4
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
Think about the differences between functions and methods (in Java)
Encoding and Decoding example in Java
StringBuffer and StringBuilder Class in Java
Check Java parameters in Kubernetes pods
Studying Java 8 (StaticIF and Default methods)
Understanding equals and hashCode in Java
Java generics (defines classes and methods)
Hello world in Java and Gradle
How to encrypt and decrypt with RSA public key in Java
Difference between final and Immutable in Java
Java static
JAVA learning history final modifier and static modifier
[Java] for Each and sorted in Lambda
Java programming (classes and instances, main methods)
Java methods
Comparison of thread implementation methods in Java and lambda expression description method
JAVA learning history abstract classes and methods
Java programming (static clauses and "class variables")
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
Create barcodes and QR codes in Java PDF
Detect similar videos in Java and OpenCV rev.2
[Java] Personal summary of classes and methods (basic)
Parallel and parallel processing in various languages (Java edition)
Difference between next () and nextLine () in Java Scanner
[Swift vs Java] Let's understand static and final
Differences in writing Java, C # and Javascript classes
Capture and save from selenium installation in Java
Detect similar videos in Java and OpenCV rev.3
[Java] Understand in 10 minutes! Associative array and HashMap
Basics of threads and Callable in Java [Beginner]
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Upload and download notes in java on S3
Encrypt / decrypt with AES256 in PHP and Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
How to access Java Private methods and fields
Java class methods
Java static story
Partization in Java
Java version check