[Easy-to-understand explanation! ] How to use Java overload

1. Prior knowledge

-[Latest] How to build Java environment on Ubuntu

-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)

-[Easy-to-understand explanation! ] How to use Java instance

-[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)

As prior knowledge, the contents of the above link are required.

2. What is overload?

--Overload means defining a method with the same method name but with number of different arguments or different argument types. --If there are multiple constructors with the same number of arguments, theconstructors with the same argument type are called. --You can also call another constructor by usingthis ()from within the constructor. --The call to the constructor bythis () must be written at the beginning `of the constructor.

3. Basic writing

Test class


package package name;
public class main class name{
    public static void main(String[] args) {
        //Instance generation
Class name Variable name 1=new class name();
Class name Variable name 2=new class name(Actual argument);
Class name Variable name 3=new class name(Actual argument of a type different from variable name 2);
    }
}

Overlord class


package package name;
class class name{
    //Definition of instance variables
private type name variable name;

    //Constructor 1 (no arguments)
name of the class(){
Initialization process, etc.
    }
    //Constructor 2 (with arguments)
name of the class(Type name Argument name){
Initialization process, etc.
    }
    //Constructor 3 (with different arguments than constructor 2)
name of the class(Type name Argument name){
Initialization process, etc.
    }
}

--Basic overload is described as above.

4. Preparation

01.png

  1. Start Eclipse and select [File (F)]-> [New (N)]-> [Java Project]. 02.png
  2. Enter Test1 for the project name and click the Done button. 03.png
  3. Select [File (F)] → [New (N)] → [Class]. 05.png
  4. Enter Test1 for the package and name and click the Done button. 06.png
  5. Confirm that Test1.java has been created. 01.png Enter Test1 in the package, enter TestOverload in the name, and click the Finish button in the same way as in 6.3. 02.png
  6. Success if Test1.java and TestOverload.java are created.

5. Description example

--The advantage of using overload is that you don't have to remember multiple methods with different names. --For example, it can be used when there are people who have confirmed attendance andpeople who have not decided in advance, such asconfirmation of attendance`. (It can be completed only with the constructor without creating three methods of attendance, absence, and undecided.)

Test1.java


//Attendance confirmation system
package Test1;
public class Test1 {
    public static void main(String[] args) {
        //Instance generation
    	TestOverload to1 = new TestOverload("A");
    	TestOverload to2 = new TestOverload("B",1);
    	TestOverload to3 = new TestOverload("C",2);

    	System.out.println("-----April 1st-----");
    	//View the contents of the instance
    	to1.showAttendance();
    	to2.showAttendance();
    	to3.showAttendance();
    }
}

TestOverload.java


package Test1;
public class TestOverload {
    //Definition of instance variables
    private String name;
    private int attend;

    //Constructor 1 (no arguments)
    TestOverload(){
    	this("Not entered",0);
    }
    //Constructor 2 (with arguments)
    TestOverload(String name){
    	this(name,0);
    }
    //Constructor 3 (with different arguments than constructor 2)
    TestOverload(int attend){
    	this("Not entered",attend);
    }
    //Constructor 4 (2 arguments)
    TestOverload(String name,int attend){
    	this.name = name;
    	this.attend = attend;
    }

	//Display method
    void showAttendance(){
    	String str = "";
    	switch(attend) {
    	case 0:
    		str += "Undecided";
    		break;
    	case 1:
    		str += "Attendance";
    		break;
    	case 2:
    		str += "Absence";
    		break;
    	}
    	System.out.println(name+"Is"+str);
    }
}

--Copy the above sentence, specify S-JIS as the character code, save the file name as Test1.java, TestOverload.java, and execute it. ↓ ↓ 03.png

6. Related

-[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse -[Even beginners can do it! ] How to write Javadoc -[Easy-to-understand explanation! ] How to use Java encapsulation

Recommended Posts

[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java instance
[Easy-to-understand explanation! ] How to use Java polymorphism
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Easy-to-understand explanation! ] How to use Java encapsulation
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
How to use Java classes, definitions, import
[Java] [Maven3] Summary of how to use Maven3
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Java] How to use the Calendar class
try-catch-finally exception handling How to use java
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use Twitter4J
How to use active_hash! !!
How to use hidden_field_tag
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Thread.sleep to pause the program