We are training Java using "Introduction to Java that is refreshingly understood, 2nd edition". I think this book is pretty straightforward, but it's still an object-oriented explanation, and it seems that it's often not hungry when it comes to "instances."
The book states:
"If you think that the class is the same as the" mold "in the plastic model factory ..." (P.302) "It is" * instance * "that operates in the virtual world, and the mold for creating that instance is" * class * ". (P.303)
Note: The term "virtual world" here means JVM. (P.283)
It's an abstract story, so I think it's a good way to get an image. However, when it comes to actually using it, I think it needs a little more specific explanation.
It is intended for the following people.
--Beginners learning Java. --Those who have learned about instances and statics, but are not comfortable with them. --Especially those who are studying Part II of "Introduction to Java that can be understood clearly 2nd edition" (hereinafter referred to as "book").
The purpose is to give you an overview of how the JVM works in memory and to give you a better understanding of how to code it.
--Class
The following items are excluded.
--Encapsulation, access modifier
The class is loaded into the JVM's memory when it is to be used by the application. Programmers don't have to worry because Java loads the classes.
Cleric.java
class Cleric {
private String name;
private int hp;
public Cleric(String name, int hp) {
this.name = name;
this.hp = hp;
}
public String toString(){
return this.name + " HP:" + this.hp;
}
}
↑ Image ↓
With this alone, you can only load the class and not the members. This is because it is not instantiated, only name and hp are defined, and neither name nor hp is allocated in memory.
Let's use new.
ClericQuest.java
class ClericQuest {
public static void main(String[] args) {
Cleric cleric1 = new Cleric("John", 50);//New here
System.out.println(cleric1.getName());//Access the new instance
}
}
↑ Image ↓ By doing new, the instance will be secured in the memory of the JVM. Members are also in it. Now you can use name and hp as well.
You can create multiple instances.
ClericQuest.java
class ClericQuest {
public static void main(String[] args) {
Cleric cleric1 = new Cleric("John", 50); //The first new
Cleric cleric2 = new Cleric("Taro", 55); //Second new
Cleric cleric3 = new Cleric("Mary", 65535); //Third new
System.out.println(cleric1);
System.out.println(cleric2);
System.out.println(cleric3);
}
}
↑ Image ↓
Members of each instance are allocated separately in memory. In this example, the name of cleric1 does not affect the name of cleric2.
The above omits the explanation of static, so we will explain static.
static is translated as "static". It is reserved from the beginning and cannot be increased or decreased. On the other hand, "dynamic" means that it can be increased or decreased at will. The explanation so far has been about dynamic members (= non-static members) that can increase the number of instances by new.
Static members are allocated in the JVM's memory when the class is loaded.
Cleric.java
class Cleric {
private String name;
private int hp;
private static int countMember = 0; //The number of instances of the Cleric. static
public Cleric(String name, int hp) {
this.name = name;
this.hp = hp;
Cleric.countMember ++;
}
public String toString(){
return this.name + " HP:" + this.hp;
}
public static int getCountMember(){
return Cleric.countMember;
}
}
ClericQuest.java
class ClericQuest {
public static void main(String[] args) {
System.out.println("The number of people" + Cleric.getCountMember());//0 is displayed
Cleric cleric1 = new Cleric("John", 50);
Cleric cleric2 = new Cleric("Taro", 55);
Cleric cleric3 = new Cleric("Mary", 65535);
System.out.println(cleric1);
System.out.println(cleric2);
System.out.println(cleric3);
System.out.println("The number of people" + Cleric.getCountMember());//3 is displayed
}
}
↑ Image ↓
Instantiation does not increase static members.
As a result, static members have the following characteristics:
If static isn't tied to an instance, does it make sense to put it in a class? → When you think about class design such as encapsulation, it makes sense to make it a member of the class.
It says "1. It is not tied to an instance", but is it consistent with "3. It is shared by all instances"? → Since "1. It is not linked to the instance", if it is set to public static, it will be shared by the entire program, but if it is set to private static, it can be accessed only from the same class, so it is described like this.
It says "1. It is not tied to an instance", but you can write it as "instance". "Static member name". → The description "instance". "Static member name" does not cause a compile error, but it is not correct. Should be "class name". "Static member name". (Of course, if it is your own class, you do not need the class name part.)
You're not using toString, right? → It's fun for future study.
I think understanding the instance is one of the early stumbling blocks. If you understand this, you can understand other Java-like programming languages.
I'm talking about classes and instances, but I think understanding static will help you better understand instances.
Recommended Posts