These are ** terms ** and ** commentary collections ** that I, as a super-dreadnought beginner, have put together so that I can understand them. Getting Started with Java-Chapter 8-What do "instances" and "classes" mean? How is it working? However, it is summarized so that it can be understood.
-** Procedure for creating an object **
① Define a class ② Create an object based on the class.
--Classes and objects are completely different.
--The active in the virtual world
is " instance ".
object ** Ambiguous ** that is sometimes used to refer to a class.
instance It is not a mold (class), but a strict indication of the entity that is active in the virtual world created from that mold.
-** Instantiation ** Creating an instance from a class.
-** [Relationship between instance and class] ** It is ** "instance" ** that plays an active role in the virtual world, and the mold for creating that instance is ** "class" **.
"Character class" "God class"
--"Class declaration" --"Declaration of attributes" --"Constant field"
Hero.java
1 public class Hero {
2 }
Hero.java
1 public class Hero {
2 String name;
3 int hp;
4 }
-** Field **
A ** variable ** declared ** inside a class block
.
-** Field Declaration **
To declare an attribute, write ** variable declaration ** in class block
.
*** "name" ** and ** "hp" ** are ** fields **.
Prefix the field declaration with ** final **. By adding final, value cannot be rewritten
.
teisuu.java
1 public class Matango {
2 int hp;
3 final int LEVEL = 10;
this
It means a ** instance ** of "self "
.
--The dot "." Has the same meaning as "no".
Matango.java
1 public class Matango {
2 int hp;
3 void sleep() {
4 this.hp = 100;
5 System.out.println( this.name + "Sleeped and recovered!");
6
Matango.java
this.hp = 100;
-** Means that the value 100 is assigned to the hp field of your own instance **.
-** This is not omitted. ** ** Unexpected behavior may occur. When using a field, explicitly add this.
The elements in the class, " field "
and " method "
.
① You will be able to ** create an instance ** based on that class. ② The type of variable that contains the ** instance born from that class ** becomes available. Example) Define Hero class → Hero type variables can be used.
-** Class type ** ** Type ** that can be used by defining a class.
-** Instance ** is usually used by putting it in ** class type variable **. Example) ** Hero h; **
-** Reasons for using class variables ** This is to programmatically identify one specific instance from among multiple instances with the same name that can exist in the virtual world.
--God class is a class ** that has a ** main method. --The main method is the method used when ** executing the program **. (Programs created in other classes such as Hero class will work by using the main method)
Main.java
public static void main(String[] args) {
--"Instance creation" --"Assigning a value to a field" --"Call method of instance"
-** [Class name variable name = new class name ();] ** Example) ** Hero h = new (); **
--The instance is created in the part called "new Hero ()" ** on the right side, and the ** instance created by "=" is assigned to the type variable **.
-** [Variable name. Field name = Value;] **
Main.java
h.name = "Minato";
--Grammar for assigning values to the field of hero h See Listing 8-12 on page 319.
Hero.java
1 public class Hero {
2 String name;
3 int hp;
4 void attack();
5 void run();
Main.java
1 public class Main {
2 public static void main (String[] args) {
3 Hero h = new Hero () ;
4 h.name = "Minato" ;
5 h.hp = "100" ;
6 System.out.println("Brave" + h.name + "Was born!");
7 }
8 }
Description of ** "h.name =" Minato ";" ** of Main class
--h is the "h" of Hero h. It is a variable "h" of type Hero.
--name is a ** field ** defined in the Hero class.
-"Minato" is a ** value **.
-** [Variable name. Method name ()] **
Method.java
h.sit(5);
h.slip();
h.sit(25);
h.run();
--h is ** h (variable) ** of Hero type h. --sit, slip, sit, run are ** method names **. --The "5" and "25" passed as arguments are ** values **.
--Use "new" to create an instance. --When using a field, "variable name.field name" --When calling a method, "variable .method name ()"
What do "instances" and "classes" mean in Getting Started with Java-Chapter 8-? Also, how is it working? I made it so that you can understand.
A refreshing introduction to Java-Second Edition-Impress Publishing, Inc. Kiyotaka Nakayama / Daigo Kunimoto
Recommended Posts