[JAVA] Instance concept, class type variables, constructors, static members

What is a class type variable?

Details at the time of instantiation


//Generate instance Darth Vader
StChara dath = new StChara();//① ・ ②

//Substitute field value
dath.name = "Darth Vader";//③
dath.focelevel = 100;//③
dath.status = "Sir Dark";//③

** (Thinking) ** スクリーンショット 2018-02-25 10.35.26.png

① Secure the memory area of the instance and create the field. ② Create a link to the memory area created in ①. Now you can access the memory area created in ①. ③ Substitute the field value.

Class variables are reference types


//Create dath2 class variable
StChara dath = new StChara();
dath.name = "Darth Vader";
dath.focelevel = 100;
dath.status = "Sir Dark";
Stchara dath2;//(1) Define the class variable dath2.
dath2 = dath;//(2) Substitute the dath link destination for dath2. (Because new instance is not created with new)

** (Thinking) ** スクリーンショット 2018-02-25 13.39.22.png

Since the class variable is a reference type variable, the link destination is assigned.

Use class type variables for fields

//Hero class
public class Hero {
	String name;
	int hp;
	Sword heroSword;//(1) Create a sword type heroSword field.
	void attack(){
		System.out.println(this.name +"Is"+this.heroSword.name+"Attacked with!");//③
		System.out.println("To the enemy"+this.heroSword.damage+"Damaged points! !!");//③
//sword class
public class Sword{
	String name;
	int damage;
//main class
public class Main {
	public static void main(String[] args) {
		Sword sword = new Sword();
		sword.name = "Flame sword";
		sword.damage = 10;

		Hero hero = new Hero();
		hero.name = "Minato";
		hero.hp = 100;
		hero.heroSword = sword;//(2) Assign the sword instance to the heroSord attribute of the hero instance.

** (Output result) **

Minato attacked with a sword of fire!
Inflicted 10 points of damage on the enemy! !!

** (Thinking) ** (1) Declare the Sword type class variable "heroSword" in the Hero instance. (2) Substitute the sword instance for heroSword. (3) Since the sword instance is assigned to the class variable heroSword, "this.herosword.name" has the same meaning as "sword.name", and the "name" and "damage" of the sword instance can be referenced.

** (Supplement) ** "Relationship in which one class uses another field" = "has-a relationship". In the above code, it is "hero has-sword".

Use class type variables in method arguments and return values

//Create a Hero instance
Hero hero1 = new Hero();
hero1.name = "Minato";
hero1.hp = 100;
//Create a Hero instance
Hero hero2 = new Hero();
hero2.name = "Asaka";
hero2.hp = 100;
//Create a Wizard instance
Wizard wizard = new Wizard();
wizard.name = "Sugawara";
wizard.hp = 50;
public class Wizard {
	String name;
	int hp;
	void heal(Hero heroX) {;//By using a class variable as an argument, you can assign an instance. By assigning an instance, you can refer to the field value of the instance.
		System.out.println(heroX.name+"Recovered 10 HP!");
		System.out.println(heroX.name+"HP is"+heroX.hp);
		System.out.println(this.name+"HP is"+this.hp);//Heals the hero, and the Wizard himself.


** (Output result) **

Recovered 10 Minato's HP!
Minato's HP is 110
Sugawara's HP is 60
Recovered 10 HP of Asaka!
Asaka's HP is 110
Sugawara's HP is 70

What is a constructor?

Constructor concept

** (Thinking) ** The initial processing (value assignment, method) to be executed when an instance is created is called a constructor.

** (Declaration method) **

name of the class(Variable that receives the argument){  
  this.Variable that receives the argument = value;

Can be declared with. At this time, the specification of the return value including void is NG.


public class Hero {
	String name;
	int hp;

	Hero(String name) {//constructor
		this.hp = 100;
		this.name = name;

Constructor overload

** (Thinking) ** Multiple constructors with the same name can be used by changing the argument type and the number of arguments. Also, when overloading, you can call another constructor and use it.

this(Argument name);


public class main {
	public static void main(String[] args){
	  Cleric a = new Cleric("clergyman",10,10);
	  Cleric b = new Cleric("clergyman",10);
	  Cleric c = new Cleric("clergyman");

public class Cleric {
	String name;
	int hp = 50;
	final static int HPMAX = 50;
	int mp;
	final static int MPMAX = 10;

	public void selfAid() {
		System.out.println(this.name + "Chanted self-aid!");
		this.hp = HPMAX;
		this.mp -= 5;
		System.out.println("HP has recovered to the maximum.");
	Cleric(String newName,int newHp,int newMp){
	  this.name = newName;
	  this.hp = newHp;
	  this.mp = newMp;

	Cleric(String name,int hp){
		this(name, hp, Cleric.HPMAX);//Since the constant field is a static member that calls other processing with this here, it can be called even before the instance is created.

	Cleric(String name){

** (Output result) **

Clergy A chanted self-aid!
HP has recovered to the maximum.
Clergy B chanted self-aid!
HP has recovered to the maximum.
Clergy C chanted self-aid!
HP has recovered to the maximum.

Static member

The idea of static members

** (Thinking) ** Even if multiple instances are created from the same class, it is necessary to define common values and methods (initial values, etc.) as static members.

** (Reference) In the previous Darth Vader diagram ... ** スクリーンショット 2018-02-25 12.01.51.png

Static field

** (Definition method) ** In instance class static data type variable name // create static field

//Stchara class
static int firstLevel;

In the main class instance class name.variable name = value // assign value to static field

//Stchara class
Stchara.firstLevel = 1;

** (Effect obtained by defining) ** -By creating a static field, the value of the field can be called with class name.variable name. (The value of the field is held by the class itself) -Class variables become available before the instance is created.

Static method

** (Definition method) **

static return value method name{
Processing content

** (Effect obtained by defining) ** -By creating a static method, you can call the method with class name.method name. (The method is held by the class itself) -Methods are available even before the instance is created.

** (Notes on static methods) ** -Be sure to use static fields as the fields to be used.

Recommended Posts

Instance concept, class type variables, constructors, static members
[Java] Classes, constructors, static members
Internal class type: With or without static
[Java] Differences between instance variables and class variables
Java programming (static clauses and "class variables")
[Ruby] Basic knowledge of class instance variables, etc.
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Introduction to Java] Variable scope (scope, local variables, instance variables, static variables)
[Java Silver] What are class variables instance variables and local variables?