To not be a static uncle

Introduction

Now that I've learned a bit about Java, I'd like to write about static modifiers. This article is the 20th day article of SLP_KBIT Part 2 Advent Calendar 2016 --Qiita.

Please point out any mistakes.

What is static uncle

Some people make heavy use of static because static methods can be called directly from the class without creating an instance. We call such people "static uncle" or not.

What is static

If you add a static modifier to a method or member variable, it becomes a static method or static variable. What changes with the static modifier is that you can access the method and member variables without new. In other words, static is -When defining a common method -When defining common properties ・ When you want to share common properties among multiple classes It is a qualifier used for.

Why you shouldn't abuse static

There are various reasons for this, but for example, static methods and variables will affect everything that is referenced elsewhere if the value of the variable is changed in one place. Therefore, abusing static can be confusing.

How should static be used

Non-static property methods

Let's explain while actually looking at the code.

Human.java



public class Human {
	private String name;
	private int satiety;	//Satiety
	
	public Human(String name, int satiety) {
		this.name = name;
		this.satiety = satiety;
	}
	
	public void eat(int amountfood) {
		this.satiety += amountfood;
	}
}

The code above is a Human class for creating a human object. This class has two properties and two methods, all of which are non-static properties and methods. Non-static property methods are instance-specific. The "name" is unique to the instance because it is not that "humans" have a common name, but that each "individual" has it. In addition, "hunger" and "eating action" are also carried and taken individually by each person, so they are unique to the instance.

Static property method

Now, let's add a static property to the Human class.

Human.java



public class Human {
	static int count_Human = 0;
	private String name;
	private int satiety;	//Satiety
	
	Human(String name, int satiety) {
		this.name = name;
		this.satiety = satiety;
		count_Human++;
	}
	
	public void eat(int amountfood) {
		this.satiety += amountfood;
	}
}

Added the static property "Number of people". When the Human method is called, it increases the "number of people". So why is the "number of people" static? That's because "number of people" is a class-specific property, not an instance-specific property. What is the "name" of "human"? I don't understand. Because the "name" belongs to the "individual". Then, what is the "number of people" of "humans"? You can answer when asked. Because "number of people" is a property of the "human" class. In this way, use static when you want to create class-specific properties and methods.

Summary

Static is useful, but abuse can be confusing as the values don't match and you don't know what code you wrote. So, let's use static well and do our best not to become a static uncle.

Quote: [Java] What is static?

Recommended Posts

To not be a static uncle
Does Java Need to Learn Anymore?
To not be a static uncle
There seems to be a word "de-Java".
Minecraft BE Tap with a wooden sword to jump
Docker error: service must be a mapping, not a None Type.
Find a Switch statement that can be converted to a Switch expression
Convert an array that may be null to a stream
A collection of patterns that you want to be aware of so as not to complicate the code
How to leave a comment
Programming can be a god.
Pass a variable to Scope.
[java] Reasons to use static
OkHttp3 should be a singleton
20 Corresponds to static method invocation
To write a user-oriented program (1)
What to do about the "cannot be read or is not a valid ZIP file" error
How to insert a video
How to create a method
Resultset's next () is not a "method to determine if there is a ResultSet next".
File transfer to virtual environment that could not be solved even after trying for a day: Memorandum