[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]

Object-orientation

An orientation that exists so that programs can be easily assembled and modified. A form that makes each person introduce themselves as if they were in the program.

Object (alias: instance)

It can be divided into information and behavior. The types of information include name and age. The types of behavior include hello and walk.

Class (part that organizes methods)

Means the blueprint for an instance.

instance

When creating an instance, it is basically assigned to a variable and used. "Class type variable name = new class name ();"

Instance field

A variable that stores information. It is basically defined at the top of the class.

class Person{
 public String name;
}

As a way to call

/*<Main.java>At*/
...
Person person1 = new Person();
person1.name = "Suzuki";

System.out.println(person1.name);

Instance method

As a general definition method ↓

class Person {
 public void hello(){   //Be careful because "static" is not used
  System.out.println("Hello");
 }
}

As a way to call

/*<Main.java>At*/
Person person1 = new Person();
Person person2 = new Person();
person1.hello();
person2.hello();
/*Note that it can only be called after the instance has been created!*/

How to use this

By using this.variable name, it can be specified as a variable of this class. [Example]

class Person{
 public String name;
 public void hello(){
  System.out.println ("Hello"+ this.name + "is");
/*Here, it will be replaced with the following "Suzuki" ↑*/
 }
}
/*<Main.java>At*/
Person person = new Person();
person.name = "Suzuki";
person.hello(); //← Output here!

constructor

A method that is automatically called after an instance is created using new. Be careful because there are the following rules as definitions. <1> Constructor name has the same class name <2> Do not write the return value! (also void)

class Person{
 public String name;
 Person (String name){
/*↑ Constructor name*/
  this.name = name;
 }
}

How to call ↓

/*<Main.java>At*/
Person person = new Person("Suzuki");
System.out.println(person.name);

Write a self-introduction statement

class Person{ /*First defined at the top of the class*/
 public String firstName;
 public String lastName;
 public int age;
 public double height;
 public double weight;

/*Define multiple in the constructor and be careful not to make a mistake in the order!*/
 Person(String firstName,String lastName,int age, double height, double weight){
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.height = height;  
  this.weight = weight;
 }

 public String fullName(){
  return this.firstName + " " + this.lastName;
 }

 public double bmi(){
  return this.weight/this.height/this.height;
 }

/*Define printData for output! !! ↓*/
 public void printData(){
  System.out.println("my name is"+this.fullName()+"is");
  System.out.println("Age is"+this.age+"I'm old");
  System.out.println("BMI"+Math.round(this.bmi())+"is");
 }/*After fullName and bmi()Don't forget! / Math.round is for rounding*/
}
 

Call ↓

/*<Main.java>At*/
...
Person person1=new Person("Kate","Jones",27,1.6,50.0);
person1.printData();
...

Overload

The overload here means When person1 has no middle name and person2 has a middle name It can be solved by defining two constructors. [Example]

class Person{
 //...Abbreviation
 public String middleName; //Plus where the instance field is defined.

 Person(String firstName,String lastName, ...); //First pattern
 //...* Omitted
 Person(String firstName,String middleName,String lastName, ...); //Second pattern
 //...* Omitted

The above "* omitted" is almost duplicated, so the second one should be abbreviated.

...
 this(firstName,lastName,...weight); //Call other constructors here
 this.middleName = middleName; //Write what you want to add here

Looking back and being careful

・ Because it is difficult to decide whether to add () or not. I will write while checking carefully. ・ In the previous article, I received advice on how to use space and indentation. I wanted to get into the habit.

Recommended Posts

[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
[Beginner] Java method / class / external library [Note 23]
[Java] Instance method, instance field, class method, class field, constructor summary
Muscle Java Object Oriented Day 1
Java, instance starting from beginner
Muscle Java Object Oriented Day 2 ~ Inheritance ~
[Java] Object-oriented syntax-class / field / method / scope
[Java beginner] == operator and equals method
Java overload constructor starting from beginner
[Technical Note] What is Object Oriented?
Java beginner design pattern (Factory Method pattern)
Java method
java (method)
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
java beginner 3
[Java] Overload
java beginner
Java method
[Java] method
[Java] method
[Java SE 11 Silver] Arrays class method summary [Java beginner]
Java starting from beginner, class declaration / object generation
[Java beginner] println method without collection type specification