[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.

When creating a class that instantiates an object in Java

-** Constructor **

It seems that you write it roughly, but it is troublesome to write code every time.

Solution (Mac OS)

Conclusion: Let's generate it automatically with ** command + n ** without touching it! !!

Let's take the Person class, which has a person's name, age, and ID as member variables, as an example.

** Person class **

public class Person {
    private int PersonId;
    private String PersonName;
    private int PersonAge;

    public Person(int PersonId, String PersonName, int PersonAge) {
        this.PersonId = PersonId;
        this.PersonName = PersonName;
        this.PersonAge = PersonAge;
    }

    public Person() {
    }

    public int getPersonId() {
        return PersonId;
    }

    public String getPersonName() {
        return PersonName;
    }

    public int getPersonAge() {
        return PersonAge;
    }

    public void setPersonId(int PersonId) {
        this.PersonId = PersonId;
    }

    public void setPersonName(String PersonName) {
        this.PersonName = PersonName;
    }

    public void setPersonAge(int PersonAge) {
        this.PersonAge = PersonAge;
    }
}

manner

-** Constructor, create default constructor **

    public Person(int PersonId, String PersonName, int PersonAge) {
        this.PersonId = PersonId;
        this.PersonName = PersonName;
        this.PersonAge = PersonAge;
    }

    public Person() {
    }

Select Constructor with command + n

スクリーンショット 2020-04-25 23.15.06.png

** Select a member variable ** and click OK to automatically generate the constructor. If ** no variables are specified **, the default constructor will be automatically generated. スクリーンショット 2020-04-25 23.14.36.png

-** Create Getter / Setter **

    public int getPersonId() {
        return PersonId;
    }

    public String getPersonName() {
        return PersonName;
    }

    public int getPersonAge() {
        return PersonAge;
    }

    public void setPersonId(int PersonId) {
        this.PersonId = PersonId;
    }

    public void setPersonName(String PersonName) {
        this.PersonName = PersonName;
    }

    public void setPersonAge(int PersonAge) {
        this.PersonAge = PersonAge;
    }

Select Getter / Setter with command + n スクリーンショット 2020-04-25 23.18.42.png Select a member variable and click OK to automatically generate Getter / Setter for each variable. スクリーンショット 2020-04-25 23.21.46.png

in conclusion

This is the easiest because you don't have to write get (), set () many times anymore.

Thank you very much.

Recommended Posts

[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
Getters and setters (Java)
Store in Java 2D map and turn with for statement
Java encapsulation and getters and setters
I can't create a Java class with a specific name in IntelliJ
Java + OpenCV 3.X in IntelliJ IDEA
Command line that can create a directory structure for building a Laravel environment with Docker in one shot
I wrote a Lambda function in Java and deployed it with SAM
Java --Introduce CheckStyle plugin to IntelliJ IDEA and reflect it in formatter
[Java] for Each and sorted in Lambda
[Java] Reading and writing files with OpenCSV
Optimize Java import declarations in IntelliJ IDEA
Split a string with ". (Dot)" in Java
Reading and writing gzip files in Java
For my son who started studying Java with "Introduction to Java" in one hand
Read a string in a PDF file with Java
Create a CSR with extended information in Java
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
[Java] Create a collection with only one element
Differences in writing Java, C # and Javascript classes
Prepare a scraping environment with Docker and Java
Encrypt / decrypt with AES256 in PHP and Java
[Review] Reading and writing files with java (JDK6)
Guidelines for writing processing when a value exists / does not exist in Java Optional
A study method for inexperienced people to pass Java SE 8 Silver in one month
A memo when I tried "Talking about writing a Java application in Eclipse and publishing it in Kubernetes with a Liberty container (Part 1)"