Java-Class Instance Creation Part.3

About how to write an article

I don't refer to class slides, so I think it's explained from a different perspective. Maybe I'm saying it (should).

Please let me know if you may make a mistake because it is an ad lib!

It consists of 4 parts

Part.1 Try to instantiate it Part.2 Cleaner with constructor Part.3 Let's control the access and limit the incoming value ← Imakoko Part.4 Try using class variables / methods

Tether

Part.2 implemented the constructor. A constructor is a special method that is executed only once when an instance is created. The constructor can be implemented by writing the name of the class.

DigitalStudentCard.java


class DigitalStudentCard{
  //Student number
  int num;

  //name
  String name;

  //constructor
  DigitalStudentCard(int num, String name){
    //Argument num to instance num(this.num)Substitute to
    this.num = num;

    //Also
    this.name = name;
  }

  //Method for output (introduce yourself)
  String getSelfIntroduction(){
    return "Student number: " + num + "name: " + name);
  }
}

The constructor is almost the same as the method, so you can write it in a similar way.

Let's restrict access!

When I hear about access restrictions, I think it feels like something like binding ... </ b>. It's like that. You can restrict access to variables, methods, and constructors by adding private or public! !!

I sometimes call these keywords access modifiers </ b> Most people say, "Why don't you make it private?" Or "Isn't it public?"

The types of such access modifiers are as follows.

Modifier efficacy
public Can be accessed from anywhere
(Do not write anything) It can be accessed from anywhere in the package. (I will explain someday)
protected extendsOrimplementsIt can be accessed only when there is a parent-child relationship with. (I will explain someday)
private It can be accessed only from that class or instance.

I will write

Let's add the private access modifier to num and name.

DigitalStudentCard.java


class DigitalStudentCard{
  //Student number
  private int num;

  //name
  private String name;

  //constructor
  DigitalStudentCard(int num, String name){
    //Argument num to instance num(this.num)Substitute to
    this.num = num;

    //Also
    this.name = name;
  }

  //Method for output (introduce yourself)
  String getSelfIntroduction(){
    return "Student number: " + num + "name: " + name);
  }
}

Write to use this.

class UseClass{
  public static void main(String[] args){

    //Instance generation
    DigitalStudentCardtanaka = new DigitalStudentCard(118114, "Kakuei Tanaka");

    //Create another instance
    DigitalStudentCardnoguchi = new DigitalStudentCard(118514, "Hideyo Noguchi");

    //output
    System.out.println(tanaka.getSelfIntroduction);
        //Student number:118114 Name:Kakuei Tanaka

    System.out.println(noguchi.getSelfIntroduction);
        //Student number:118514 Name:Hideyo Noguchi
  }
}

Nothing changes in this example, but the following writing style is NG.

tanaka.num = 114514;
    //Compile error

tanaka.name = "Kakuei Tanaka";
    //Compile error

Since num and name are restricted by the private access modifier so that they can only be accessed inside the instance, they cannot be accessed.

Why do you need such a hassle?

"It's convenient if you can access it from anywhere, and it's easy."

Certainly, it seems that it is better to write in a way that you like without restrictions. But you can do whatever you want </ b> doesn't sound like it?

so. It's dangerous. (Small vocabulary)

If this is Magimon's student ID card, it would be irresistible to rewrite the student ID number or name without permission. Even if you change it, there should be rules such as numbers and names that you want to follow.

Let's implement a method called Getter / Setter </ b> to provide such a rule.

Make a getter / setter

Please be assured that just saying "it's called this way" doesn't mean writing a special method </ b>. That's the way I've often seen it.

It is safe to name getters / setters get〇〇 () and set〇〇 ().

DigitalStudentCard.java


class DigitalStudentCard{
  //Student number
  private int num;

  //name
  private String name;

  //constructor
  DigitalStudentCard(int num, String name){  
    //I made it so much that I use a getter
    setNum(num);

    //Also
    setName(name);
  }

  //Method for output (introduce yourself)
  String getSelfIntroduction(){
    return "Student number: " + num + "name: " + name);
  }

  // -----add to-----

  //A method that just returns num (getter)
  int getNum(){
    return num;
  }

  //Method that just returns name (getter)
  String getName(){
    return name;
  }

  //Method to set num (setter)
  void setNum(int number){
    //Take the student ID number of our school as an example
    //Somehow I'm getting older
    int year = number/1000;

    //Prohibit values above 119
    if(year > 118){
      year = 118;
    }

    //Extract only number-like cousins
    number = number%1000;

    //I want to fit it in 3 digits, so if the value exceeds 999, set it to 999.
    if(number > 999){
      number = 999;
    }

    //Stick and return
    return year*1000 + number;
  }

  //Method to set name (setter)
  void setName(String name){
    this.name = name;
  }
}

Now let's call these methods.

class UseClass{
  public static void main(String[] args){
    //Instance generation
    DigitalStudentCardtanaka = new DigitalStudentCard(118114, "Kakuei Tanaka");

    //Show the value of the num variable
    System.out.println(tanaka.getNum());
        // >> 118114

    //Show the value of the name variable
    System.out.println(tanaka.getName());
        //Kakuei Tanaka

    //Change the value of the num variable
    tanaka.setNum(200, 1000);
        //Try to enter a large value on purpose

    //Change the value of the name variable
    tanaka.setName("Kakuei Tanaka");
        //I couldn't think of anything, so what was hiragana?


    System.out.println(tanaka.getNum());
        // 118999
    System.out.println(tanaka.getName());
        //Kakuei Tanaka
  }
}

By doing this, I want only nun! I want only name! You can handle this by using getNum () or getName ().

If you want to change your student ID number due to unforeseen circumstances, or if you get married and your surname changes, you can set it using setNum () or setName ().

Since it would be a problem if the student ID number could be entered with a messy value, we have created a mechanism to confirm that it is an appropriate value. Since the value that can be entered in the variable num can be limited, it can be avoided to become a lawless zone.

Since setNum () and setName () are also used in the constructor, it was possible to make the assignment process only in one place. By doing this, even if there is a bug later, you only have to check one place, so it will be easier to fix it! !!

By the way

Access modifiers can also be used for methods and constructors.

private void setName(String name){
  //Something processing
}

As with variables, the range of access is limited to within that class instance.

next time

Next, let's access the same object from various places using class variables and class methods! Next → Part.4 Try using class variables / methods