Java, instance starting from beginner

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ~~ ** The mistaken ** is the center. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. ~~ In addition, I will review the Java Silver exam questions at a later date, so I will not touch on the deep part here.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

instance

I remember talking about objects and instances confusingly before, but I would like to touch on them again. First, prepare the Cat class as usual.

Cat.java


class Cat
{
  public static int sumCats = 0;

  private int age;
  private double weight;

  public Cat()
  {
    age = 0;
    weight = 0.0;
    sumCats++;
    System.out.println("A new cat has arrived!");
  }

  public void setCat(int a,double w)
  {
    age = a;
    weight = w;

    System.out.println("This cat is age" + age + "Age, weight" + weight + "It is kg.");
  }

  public static void showSumCats()
  {
    System.out.println("Now in total" + sumCats + "I have a cat.");
  }
}

Consider the case where two cats are prepared using this Cat class.

-** 2 cats ** are separate ** objects ** from the same Cat class. -Two cats have their own ʻage, weight` fields.

In the above, the two cats use the ** instance variable **, which is the field associated with each object, and the ** instance method **, which is the method associated with the object. Have. These instance variables and methods "become accessible after the object is created ( new) ".

Misunderstandings I had so far ・ Object ≒ instance

⇒Field method for each object = Instance variable method * new !! *

Class variables / class methods

However, there are some members (field methods) that you want to handle before the object is created. Therefore, members that are ** associated with the class itself ** can be declared as static and used.

public static void showSumCats () is Refer to the ** class variable sumCats ** declared aspublic static int sumCats = 0;, and ** constructor Cat () ** is called every time new is done, and it is automatically incremented. It has become. This links "how many times Cat objects were created" with "total number of cats". Since showSumCats () is static, it is a method that can be called even if" the Cat object has never been created ". Therefore, "0 cats" can be indicated.

What is this.

** Only within instance methods **, this.age and this.weight can be written to indicate only the field values associated with the object. The story of local variables is omitted.

At the end

If you don't understand well whether the variables / methods you are going to handle in the main function are class-related or object-related by static, there are quite a lot of problems to be struck here.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java, instance starting from beginner
Java starting from beginner, override
Java starting from beginner, inheritance
Java overload constructor starting from beginner
Java starting from beginner, variables and types
Java starting from beginner, nested / break / continue
Java, if statement / switch statement starting from beginner
Java, for statement / while statement starting from beginner
Java starting from beginner, class declaration / object generation
Java life starting from scratch
java beginner 4
java beginner 3
java beginner
Java, abstract classes starting from beginners
Java, interface to start from beginner
IntelliJ starting from 1
[Java] Exception instance
Java starting from beginners, logical operators / conditional operators
Getting Started with Java Starting from 0 Part 1
Java Exercise "Beginner"
For Java engineers starting Kotlin from now on
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Eval Java source from Java
Access API.AI from Java
From Java to Ruby !!
[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
[Java] Platforms to choose from for Java development starting now (2020)
Migration from Cobol to JAVA
Progate Java (Beginner) Review & Summary
Creating ElasticSearch index from Java
New features from Java7 to Java8
Connect from Java to PostgreSQL
Using Docker from Java Gradle
From Ineffective Java to Effective Java
JavaScript as seen from Java
[Beginner] Java basic "array" description
Execute non-Java instructions from Java
[Java twig] Change & add instance behavior from outside (Proxy black magic)
Solve AtCoder Beginner Contest 151 in java
Call Kotlin's sealed class from Java
Solve AtCoder Beginner Contest 150 in java
Code Java from Emacs with Eclim
Solve AtCoder Beginner Contest 153 in java
[Java beginner] About abstraction and interface
Get country from IP address (Java)
Run a batch file from Java
[Java] Remove whitespace from character strings
Akka hands-on preparation procedure from Java
[Java beginner] == operator and equals method
Access Teradata from a Java application
Solve AtCoder Beginner Contest 175 in java
Use Chrome Headless from Selenium / Java
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Solve AtCoder Beginner Contest 160 in java
Measuring instance memory usage in Java
Work with Google Sheets from Java
Solve AtCoder Beginner Contest 152 in java
[Java] Proxy setting method when starting Java