[Java] Personal summary of classes and methods (basic)

[Java] Personal summary of classes and methods (basic)

This is the engineer t-77. I personally summarized what I thought was important in studying Java. This time, it's about class instance methods.

① About class

--One of the objects that are things or things that have not yet been materialized (blueprints for things). --Java code requires at least one class. --If you think of a class as one "thing", the contents (members) of the class are roughly divided into the following two. --A "field" that handles the state and properties of things --"Methods" that handle the functions of things --The class is basically described as "sample.java" below.

sample.java


//Class declaration
class Animal
{
  //Describe the "field" that handles the state and properties of things in the class
  //"Type name Field name;Described as
  String name;
  int num;
  //Describe "method" that handles the function of the class
  //"Return type method name(Argument list){Sentence;}Described as
  void show() {
    System.out.println("The name of the animal is" + name + "is.");
    System.out.println("The registration number is" + num + "is.");
  }
}

② About the instance

――One of the objects that are things or things, and is materialized (the real thing made from the blueprint). --You need to create an instance to use the declared class. --To create an instance, follow the steps below.

  1. Declare an instance variable.
  2. Create an instance so that it can be handled by that variable. --Once you create an instance, you will be able to access the members of the class.

sample.java


//Class declaration
class Animal
{
  String name;
  int num;

  void show() {
    System.out.println("The name of the animal is" + name + "is.");
    System.out.println("The registration number is" + num + "is.");
  }
}
class Sample
{
  public static void main(String[] args)
  {
  //Declare instance variables.
  Animal animal1;
  //Create an instance.
  //When creating an instance, it becomes a "thing that can actually be handled".
  animal1 = new Animal();
  //You can declare and create an instance at the same time as follows.
  // Animal animal1 = new Animal();
  
  //Instance name.You can access the field by field name.
  animal1.name = "dog";
  animal1.num = "1";
  //Instance name.You can access the method by method name.
  animal1.show();
  }
}

console


The name of the animal is dog.
The registration number is 1.

③ About the method

--Methods are the parts that handle (process) the functions of things. --When calling a method from outside the class, use "instance name.method name". --When calling a method in a class, use "method name" or "this.method name".

sample.java


//Class declaration
class Animal {
  String name;
  int num;

  void show() {
    System.out.println("The name of the animal is" + name + "is.");
    System.out.println("The registration number is" + num + "is.");
  }
  void showAnimal() {
    System.out.println("I will introduce animals.");
    //You are calling your own method (calling from within the class).
    // show()May be.
    this.show();
  }
}
class Sample {
  public static void main(String[] args) {
    Animal animal1;
    animal1 = new Animal();
  
    animal1.name = "dog";
    animal1.num = "1";
    //The method is called with the information of animal1 (calling from outside the class).
    animal1.showAnimal();
  }
}

console


I will introduce animals.
The name of the animal is dog.
The registration number is 1.

④ Method argument and return value

--You can pass arguments (values) to the method. --There are the following two types of arguments.

(1) Variable that receives the value in the method definition => Formal argument (2) Value to be passed when calling the method => Actual argument --Return value (information) can be passed from the method body to the method caller. --For methods with no return value, use void type. --There are two types of return values as follows.

(1) When the caller uses the return value

Return type Variable name = Object name. Method name (argument list);

(2) When the caller does not use the return value

Object name. Method name (argument list);

sample.java


//Class declaration
class Animal {
  String name;
  int num;

  //Prepare String type and int type formal parameters.
  //A method with no return value.
  void show(String str, int n) {
    //Make the argument value available in the method.
    name = str;
    num = n;
    System.out.println("The name of the animal is" + str + "is.");
    System.out.println("The registration number is" + n + "is.");
  }
  //A method whose return value is a String type.
  String getAnimal() {
    System.out.println("I will introduce animals.");
    //Pass the return value to the caller.
    return name;
  }
}
class Sample {
  public static void main(String[] args) {
    Animal animal1 = new Animal();
    String name = "Tiger";
    //Pass the actual argument to the method with the information of animal1 (variables are also available).
    //A pattern that does not use the return value at the caller.
    animal1.show(name, 2);
    //A pattern that uses the return value at the caller.
    int number = animal1.getAnimal();
    //Use the return value.
    System.out.println(name + "Registration number is" + number + "is.");
  }
}

console


The name of the animal is tiger.
The registration number is 2.
I will introduce animals.
The tiger registration number is 2.

⑤ Access restrictions to members

--Access restrictions can be set for members. --The "private" member cannot be accessed from outside the class. --The "public" member can be accessed from outside the class.

⑥ Encapsulation

--A function to set access restrictions by grouping data (fields) and functions (methods) in a class and adding "private" to the members you want to protect.

sample.java


class class name{
  //Not accessible from outside the class.
private type name field name;
  //Accessable from outside the class.
public void method name{
formula;
  }
}

⑦ Method overload

--Define a method with the same method name but different argument types and numbers in the same class. --If you overload only by the difference in the return type, you will not know which one the program should access. Therefore, when overloading, be sure to "have different argument types and numbers".

sample.java


class Animal {
  String name;
  int num;

  //A method with one argument.
  void show(String str) {
    name = str;
    System.out.println("The name of the animal is" + str + "is.");
  }
  //A method with two arguments.
  void show(String str, int n) {
    name = str;
    num = n;
    System.out.println("The name of the animal is" + str + "is.");
    System.out.println("The registration number is" + n + "is.");
  }
}
class Sample {
  public static void main(String[] args) {
    Animal animal1 = new Animal();
    String name = "Tiger";
    int num = 2;
    //Call a method with one argument.
    animal1.show(name);
    //Call a method with two arguments.
    animal1.show(name, num);
  }
}

console


The name of the animal is tiger.
The name of the animal is tiger.
The registration number is 2.

This time is over.

Recommended Posts

[Java] Personal summary of classes and methods (basic)
[Java] Personal summary of conditional statements (basic)
[Java] Generics classes and generics methods
Ruby methods and classes (basic)
Java abstract methods and classes
[Java] Personal summary of classes and methods (basic)
Java generics (defines classes and methods)
Java programming (classes and instances, main methods)
JAVA learning history abstract classes and methods
Summary of Java Math.random and import (Calendar)
Java static [Personal summary]
Personal summary about Java
java (classes and instances)
Summary of Java support 2018
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Equivalence comparison of Java wrapper classes and primitive types
Basic methods of Ruby hashes
Basic methods of Ruby arrays
Summary of FileInputStream and BufferedInputStream
(Note) Java classes / variables / methods
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of Java language basics
Java methods and method overloads
[Java] Summary of for statements
Summary of Java Math class
Advantages and disadvantages of Java
[Ruby] Singular methods and singular classes
Summary of basic functions of ImageJ
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Ruby] Singular methods and singular classes
[For beginners] Explanation of classes, instances, and statics in Java
[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
[Java] Difference between assignment of basic type variable and assignment of reference type variable
About Java static and non-static methods
[For beginners] Summary of java constructor
Various methods of Java String class
[Java] Basic types and instruction notes
Java release date and EOL summary
About fastqc of Biocontainers and Java
Summary of [Java silver study] package
Basic data types and reference types (Java)
How to call classes and methods
Basic usage of java Optional Part 1
Reading and writing Java basic files
[Java] Judgment of identity and equivalence
Studying Java 8 (StaticIF and Default methods)
Basic processing flow of java Stream
Application of downcase and slice methods
Java basic data types and reference types
Summary of object-oriented programming using Java
[Basic knowledge of Java] Scope of variables
Classes and instances Java for beginners
[Java] Exception types and basic processing
Basic structure of Java source code
[Java] Various summaries attached to the heads of classes and members
I tried to summarize the methods of Java String and StringBuilder
A brief summary of DI and DI containers