Java, class field methods starting from beginners

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 not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

This time, in order to understand the basics, I will not describe any mistakes.

environment

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

Class declaration

Let's start by declaring the class. This is a quote from Easy Java 7th Edition.

declaration.txt


class class name
{
Model name Field name;
  ...
Return type method name(Argument list)
  {
Sentence;
    ...
return expression;
  }
  ...
}

Let's compare it with the actual class based on this declaration.

Cat.java


class Cat
{
  double weight;
  String color;
  //This is the type name and field declaration.
  //You now have weight and color fields.

  void setWeightColor(double w,String c)
  {
    weight = w;
    color = c;
    
    System.out.println("The weight of this cat" + weight + "I made it.");
    System.out.println("The color of this cat" + color + "I made it.");
  }

  double getWeight()
  {
    System.out.println("Update the weight of the cat.");
    return weight;
  }

  void introduce()
  {
    System.out.println("The weight of this cat" + weight + "It is kg.");
    System.out.println("The color of this cat" + color + "The color.");
  }
  //This is the description of the method. Pay attention to the return type.
  //Cat class is setWeightColor(double w,String c),getWeight()
  //And introduce()Has a method of.
}

You can see which fields and methods are. These fields and methods are called ** members ** of the class.

As an aside, class names, field names, and method names are all ** identifiers **. Therefore, if you follow the rules of identifiers (think of them as rules of variables) that you have dealt with before, you can ** any name you like **. There is no problem in the program even if the identifier does not make sense, but if you write it in consideration of readability, it will be read back later. ・ It may be easy to understand when someone reads it.

The following is an example that is (for the time being) OK. No readability. Even in such a state, depending on the specifications of the IDE (Integrated Development Environment) and text editor, the source can be written without (relatively) obstacles, so it is really a useful tool of civilization.

longLongSoLongNames.java


class UnitedKingdomOfGreatBritainAndNorthernIreland
{
  int pneumonoultramicroscopicsilicovolcanoconiosis;
  String Tetaumatawhakatangihangakoauaotamateaurehaeaturipukapihimaungahoronukupokaiwhenuaakitanarahu;
  
  void Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch()
 {
   System.out.println("The longest English word in the world is
In the name of the disease" + pneumonoultramicroscopicsilicovolcanoconiosis
    + "The longest place name in the world is"
    + Tetaumatawhakatangihangakoauaotamateaurehaeaturipukapihimaungahoronukupokaiwhenuaakitanarahu 
    + "is.")
 }
}

field

For weight and color declared in the Cat class, you can use Cat to create a new object or instance and then store the value in that object. Example:

mainCatCreate.java


Cat firstCat = new Cat();
firstCat.weight = 2.3;
firstCat.color = "Tea tiger";

You can prepare as many objects as you like, as long as you create an object properly with new.

Method

Following the above example, it would take too much time to prepare values for all objects one by one, so use the method written in the Cat class.

methodTry.java


Cat firstcat =new Cat();

firstCat.setWeightColor(2.3,"Tea tiger")

double myCatWeight = firstCat.getWeight();

System.out.println("The weight of the cat for the first time in my home" + myCatWeight + "was.");

At the end

It was a simple class, but it took about 20 minutes to write it, including checking it. If you don't write a lot, you won't be able to learn it, and I feel that mass-producing these classes is a real pleasure, so it may be a good idea to set up a number of times to create a class.

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, class field methods starting from beginners
Java, abstract classes starting from beginners
Java class methods
Java starting from beginners, logical operators / conditional operators
Java class type field
Java starting from beginner, class declaration / object generation
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
About Java class variables class methods
Java life starting from scratch
Call Kotlin's sealed class from Java
Various methods of Java String class
java (use class type for field)
Java overload constructor starting from beginner
How to use class methods [Java]
Java methods
Java methods
Java starting from beginner, variables and types
Java starting from beginner, nested / break / continue
Java, if statement / switch statement starting from beginner
Getting Started with Java Starting from 0 Part 1
Java, for statement / while statement starting from beginner
[Java] Instance method, instance field, class method, class field, constructor summary
GetInstance () from a @Singleton class in Groovy from Java
[Java] Class inheritance
IntelliJ starting from 1
java Scanner class
Java HashMap class
Java method call from RPG (method call in own class)
java (abstract class)
Programming beginners learn PHP from a Java perspective-variables-
How to get Class from Element in Java
Java anonymous class
About Java class
String class methods
For Java engineers starting Kotlin from now on
[java] abstract class
Java local class
Try calling synchronized methods from multiple threads in Java
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
Behavior when calling Java variadic methods from Scala / Kotlin / Java
[Java] Platforms to choose from for Java development starting now (2020)
Call Java from JRuby
About class division (Java)
Changes from Java 8 to Java 11
Sum from Java_1 to 100
About Java StringBuilder class
[Java] About Singleton Class
Eval Java source from Java
Java inner class review
Access API.AI from Java
Java programming (class method)
About Java String class
From Java to Ruby !!
Java programming (class structure)
Find the address class and address type from the IP address with Java
Implement Java Interface in JRuby class and call it from Java