Java starting from beginner, class declaration / object generation

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. 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 not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

This time, I will not deal with mistakes. The story of the class is important in Java, and I haven't understood it enough to make a mistake. Please forgive me for reconfirming the definition in the correct form.

environment

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

class

Hello,world!.java


class helloWorld
{
public static void main(String[] args)
  {
    System.out.println("Hello,world!");
  }
}

This is the simplest program, which produces Hello, world! Output in Java. As you can see in the first line, this time the theme is ** class (class) **. It is a function that is always handled when handling Java. According to the reference book "Easy Java 7th Edition"

What is a class? A concept used to create a program while summarizing the state and nature of things and the functions related to them.

Is. My current understanding is that class is the recognition of preparing a reference sample, so to speak, a template before dealing with specific individual objects.

object

Cat.java


class Cat
{
String name;
double weight;
double size;
String color;
int ID;
String voice;
//There is no end to the list! !!
}

I let this class declare a small part of the status of a "general cat". However, this does not include the characteristics of "my cat". Let's actually create a mycat with" my cat "status while processing the main method.

myCat.java


class myCat
{
  public static void main(String[] args)
  {
    Cat mycat = new Cat();
    //Create an object of Cat class in mycat variable
    
    mycat.name = "Tet";
    mycat.color = "brown";
    mycat.ID = 3;

   System.out.println("My cat's name is" + mycat.name + "。");
   System.out.println("color is" + mycat.color + "、" + mycat.ID + "The second cat.");
  }
}

//Output result: My cat, my name is Tet.
//The color is brown, the third cat.

A ** object ** or ** instance ** is defined in class Cat and created in mycat byCat mycat = new Cat ();.

At the end

In my next plan, I will deal with a program that outputs Hello, World!, Which is written first by anyone who touches the program. I thought that I should write it first, but as a person who entered from Python, Hello, World of Java had to use a class from the beginning and it was very difficult, so I waited so far.

In the next installment, we will deal with the ** fields ** and ** methods ** that we have omitted this time. This time, the content was thin, but I'm not so confident about the future, so I'll divide it as an index of my current ability.

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 starting from beginner, class declaration / object generation
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java overload constructor starting from beginner
[Java] Object class
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 life starting from scratch
Java, abstract classes starting from beginners
Java, interface to start from beginner
java beginner 4
[Beginner] Java method / class / external library [Note 23]
Java starting from beginners, logical operators / conditional operators
java beginner
Getting Started with Java Starting from 0 Part 1
Java method call from RPG (method call in own class)
How to get Class from Element in Java
[Java SE 11 Silver] Arrays class method summary [Java beginner]
Java file generation from apk (Bytecode Viewer expression)
For Java engineers starting Kotlin from now on
Java class methods
IntelliJ starting from 1
java Scanner class
Java HashMap class
java (abstract class)
[Java] Nested class
Java anonymous class
Java permutation generation
Java Beginner Exercises
java variable declaration
About Java class
Lombok ② Object generation
[java] abstract class
Java local class
Java Exercise "Beginner"
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
About Java setters and getters. <Difference from object orientation>
[Java] Platforms to choose from for Java development starting now (2020)
[Java] Object operation of ArrayList class (AOJ ④ Inversion of sequence)