Java-Class Instance Creation Part.4

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 Part.4 Try using class variables / methods ← Imakoko

Tether

In the previous part, access modifiers were used to restrict access from outside the class.

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);
  }
}

If you add the private access modifier, you can access variables and methods only inside the class. By restricting access, you can create getters / setters, create rules when changing values, or prohibit value changes in the first place.

What is static?

It seems that static means" static "when translated into Japanese. It's not sexual. </ s>

There will be as many instance variables and instance methods as there are instances. Static variables and static methods are not created many times, only one is created for the class.

No instance creation is required when referencing static methods and variables. </ b> You can call it like class name.variable name,class name.method name ().

I will write

Create a new CardManager class that counts the number of student ID cards issued.

CardManager.java


class CardManager{
  static int totalCardCount;
  
  static int getTotalCount(){
    return totalCardCount;
  }

  static void countUpTotalCount(){
    totalCardCount++;
  }
}

So let's add a mechanism to tell the constructor of the DigitalStudentCard class that it was issued.

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;

    //Call a CardManager method
    CardManager.coutUpTotalCount();
  }

  /*Abbreviation*/
}

This will allow you to count the number of instances of the DigitalStudentCard class created.

class UseClass{
  public static void main(String[] args){
    //Appropriately instantiate
    DigitalStudentCard tanaka = new DigitalStudentCard(118114, "Kakuei Tanaka");
    DigitalStudentCard noguchi = new DigitalStudentCard(118514, "Hideyo Noguchi");
    DigitalStudentCard sakamoto = new DigitalStudentCard(118060, "Ryoma Sakamoto");
    DigitalStudentCard oda = new DigitalStudentCard(118010, "Oda Nobunaga");

    //Try to output totalCount
    System.out.println(CardManager.getTotalCount());
        // 4
  }
}

By the way

I want to use the same value in various places! I want to use the same method! What a convenient time.

For example, it seems that methods that arrange character strings and methods that perform fixed calculation processing are organized. If you use it for a method of a class that does not have an instance variable, you do not have to bother to create an instance, so I think it is good.

But ... be careful as using a lot will waste memory.

It's not so good to have variables with a wide scope or many static variables, so if you think "that ...?", Consider redesigning it.

Recommended Posts