Getting Started with Java_Chapter 8_About Instances and Classes

Introduction

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.

How to create a virtual world

-** 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 ".

-** 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" **.

Two classes in the program

"Character class" "God class"

How to define a class

[Overview]

--"Class declaration" --"Declaration of attributes" --"Constant field"

** Class declaration **

Hero.java


1   public class Hero {
2   }
** Attribute declaration (field) **

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 **.

** Constant field **

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.

member

The elements in the class, " field " and " method ".

Class definition

① 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.

How to make a god class (class with main method)

--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) {

[Overview]

--"Instance creation" --"Assigning a value to a field" --"Call method of instance"

** Instantiation **

-** [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 **.

** Assigning a value to a field **

-** [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 **.

** Invoking method of instance **

-** [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 **.

To summarize briefly

--Use "new" to create an instance. --When using a field, "variable name.field name" --When calling a method, "variable .method name ()"

Summary

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.

References

A refreshing introduction to Java-Second Edition-Impress Publishing, Inc. Kiyotaka Nakayama / Daigo Kunimoto

Recommended Posts

Getting Started with Java_Chapter 8_About Instances and Classes
About classes and instances
About classes and instances (evolution)
Consideration about classes and instances
About Ruby classes and instances
Getting Started with Java_Chapter 5_Practice Exercises 5_4
Writing code with classes and instances
Classes and instances
[Ruby] Classes and instances
I compared classes and instances with worldly things
Getting Started with DBUnit
Getting Started with Ruby
Ruby classes and instances
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
java (classes and instances)
About the difference between classes and instances in Ruby
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Creating Ruby classes and instances
Getting Started with Ruby Modules
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Reactive Streams and the JDK 9 Flow API
Organize classes, instances, and instance variables
[Google Cloud] Getting Started with Docker
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
Classes and instances Java for beginners
Getting Started with Doma-Criteria API Cheat Sheet
Getting Started with Ruby for Java Engineers
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Java programming (classes and instances, main methods)
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Getting started with the JVM's GC mechanism
Getting Started with Micronaut 2.x ~ Native Build and Deploy to AWS Lambda ~
Getting Started with Language Server Protocol with LSP4J
Write code using Ruby classes and instances
Getting Started with Creating Resource Bundles with ListResoueceBundle
[Deprecated] Getting started with JVM GC and memory management that I didn't understand
About getting a tens digit with "(two-digit integer) / 10% 10"
Links & memos for getting started with Java (for myself)
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Using Joins with the Criteira API
Getting Started with Doma-Introduction to the Criteria API
I tried Getting Started with Gradle on Heroku
Going back to the beginning and getting started with Java ① Data types and access modifiers
About standard classes
Getting started with Java programs using Visual Studio Code
I started MySQL 5.7 with docker-compose and tried to connect
Java classes and instances to understand in the figure
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)