java (classes and instances)

Declaration of constant field

public class Matango {
	int hp;
	//int level = 10;    //field
	final int LEVEL = 10; //Constant field
}

A constant field that prevents the value from being rewritten Name with final touppercase(LEVEL)

do not omit this

public class Hero {
	String name;   //Name declaration
	int hp;        //HP Declaration
	
	public void sleep() {
		this.hp = 100;
		System.out.println(this.name + "Sleeped and recovered");
	}

It works even if this. is omitted If local variables and arguments have the same hp, they may take precedence. Add this. when using fields

Instance generation

public class Main {
	public static void main(String[] args) {
		//1. Generate a brave man
		Hero h = new Hero();
	}
}

Class name (Hero) Variable name (h) = new Class name (Hero) ();

Assigning a value to a field

public class Main {
	public static void main(String[] args) {
		//1. Generate a brave man
		Hero h = new Hero();
		//2. Set the initial value in the field
		h.name = "Brave";
		h.hp = 100;
		System.out.println("Brave" + h.name + "Was born!");
	}
}

Variable name. (H.) Field name (name) = Value ("Brave");

Recommended Posts

java (classes and instances)
Classes and instances
Classes and instances Java for beginners
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
Java programming (classes and instances, main methods)
About classes and instances (evolution)
Consideration about classes and instances
Creating Ruby classes and instances
Java abstract methods and classes
Organize classes, instances, and instance variables
Java generics (defines classes and methods)
[For beginners] Explanation of classes, instances, and statics in Java
Java and JavaScript
XXE and Java
Memorandum (Ruby: Basic Grammar: Classes and Instances)
JAVA learning history abstract classes and methods
Write code using Ruby classes and instances
[Java ~ Classes and External Libraries ~] Study Memo (6)
Getting Started with Java_Chapter 8_About Instances and Classes
Getters and setters (Java)
[Java] Thread and Runnable
HashMap and HashSet classes
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
Java true and false
[Java] Personal summary of classes and methods (basic)
String literals and instances
[Java] String comparison and && and ||
[Java] About anonymous classes
Differences in writing Java, C # and Javascript classes
Kantai Collection Java # 1 Classes and Objects [For Beginners]
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
List and happy classes
[Java] Variables and types
[Java] Overload and override
About the difference between classes and instances in Ruby
Study Java # 2 (\ mark and operator)
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
(Note) Java classes / variables / methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
Java methods and method overloads
[Java] Classes, constructors, static members
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Ruby] Singular methods and singular classes
About abstract classes in java