[Java] Object-oriented syntax-class / field / method / scope

class

Modifier

public

strictfp

Class structure

field

Person.java


//String type name,int type age field definition
public class Person {
  public String name;
  public int age;
}

Main.java


public class Main {
  public String firstName;
  public String lastName;

  public static void main(String[] args) {
    //Instantiate with new operator
    var p1 = new Person();
    p1.name = "Yamada Taro";
    p1.age = 30;

    var p2 = new Person();
    p2.name = "Ichiro Suzuki";
    p2.age = 25;

    System.out.printf("%s(%d years old)\n", p1.name, p1.age); //Taro Yamada (30 years old)
    System.out.printf("%s(%d years old)\n", p2.name, p2.age); //Ichiro Suzuki (25 years old)
  }
}

Field modifier

Field default

Method

Person.java


//show method definition
public class Person {
  public String name;
  public int age;

  public String show() {
    return String.format("%s(%d).", this.name, this.age);
    //return String.format("%s(%d).", name, age);
  }
}

MethodBasic.java


//p.Access show method with show
public class MethodBasic {

  public static void main(String[] args) {
    var p = new Person();
    p.name = "Yamada Taro";
    p.age = 30;
    System.out.println(p.show());
  }
}

Method argument

//Actual argument
getBmi(60,1.7)

//Formal argument
public double GetBmi {double weight, double height){
    return  weight / (height * height )
}

Method return value

Person.java


public class Person {
  public String name;
  public int age;

  public void show() {
    System.out.printf("%s(%d).\n", this.name, this.age);
  }
}

MethodVoid.java


public class MethodVoid {

  public static void main(String[] args) {
    var p = new Person();
    p.name = "Yamada Taro";
    p.age = 30;
    p.show();
  }
}

Method modifier

this

Method overload

//Expect optimal overload depending on the type
//But the result is
//CharSequence: Spring is Akebono
//CharSequence: Summer is night
//CharSequence: Autumn is dusk
public class OverloadAnti {

  public void show(String value) {
    System.out.println("String: " + value);
  }

  public void show(StringBuilder builder) {
    System.out.println("StringBuilder:" + builder);
  }
  public void show (StringBuffer buf) {
    System.out.println("StringBuffer:" + buf);
  }
  public void show(CharSequence cs) {
    System.out.println("CharSequence:" + cs);
  }
}
public class OverloadAntiClient {

  public static void main(String[] args) {
    var c = new OverloadAnti();
    var list = new CharSequence[] {
        "Spring is Akebono",
        new StringBuilder("Summer is night"),
        new StringBuffer("Autumn is dusk"),
    };

    for (var cs : list) {
      c.show(cs);
    }
  }
}

scope

Field-local variable collision

Scop.java


//data is covered
public class Scope {
    //Data as a field variable
  public String data = "field";

  public String show() {
    //Data as a local variable
    //Declaring this temporarily hides field variables that should be valid for the entire class
    var data = "local";
    return data;
    //If you want to access hidden field variables
    // return this.data;
  }
}

ScopeBasic.java


public class ScopeBasic {

  public static void main(String[] args) {
    var s = new Scope();  
    //show method local variables
    System.out.println(s.show()); //local
    //Local variables do not affect field variables
    System.out.println(s.data); //field
  }
}
//NG example: str1 is valid from the declared position
public class ScopeStrict {
  String str2 = str1; //Str2 cannot be declared before str1
  String str1 = “neko desu“;
}
public class ScopeStrict {
  public void show() {
    System.out.println(str); //neko desu
  }
  String str = “neko desu”;
}

StrictClient.java


public class StrictClient {
  public static void main(String[] args) {
    var s = new ScopeStrict();
    s.show();
  }
}

Block scope

public class ScopeBlock {
  public static void main(String[] args) {
    /*
    //Declare a block scope variable with the same name as a local variable
    var data = "Local scope";
    {
      var data = "Block scope"; //error
    }
    */

    {
      var data = "Block scope";
    }
    //At this point the block scope variable has been destroyed
    var data = "Local scope"; //OK
    System.out.println(data);
  }
}
//Parallel block scope
public class ScopeBlock {

  public static void main(String[] args) {
    {
      var data = "Block scope";
      System.out.println(data);
    }

    {
      var data = "Block scope 2";
      System.out.println(data);
    }
  }
}

Recommended Posts

[Java] Object-oriented syntax-class / field / method / scope
Java method
java (method)
[Java] Object-oriented syntax --class method / argument
Java method
[Java] method
[Java] Object-oriented
[Java] method
[Java] Instance method, instance field, class method, class field, constructor summary
Object-oriented FizzBuzz (Java)
[Java] Object-oriented summary_Part 1
Java8 method reference
Java variable scope (scope)
[Java] Object-oriented syntax-Constructor
[Java] forEach method
Object-oriented (Java) basics
Java variable scope
[Java] Object-oriented summary_Part 2
Java session scope
java8 method reference
[Java] Random method
[Java] split method
[Java] Object-oriented syntax-Package
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
JAVA DB connection method
Java learning 2 (learning calculation method)
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Java class type field
Java programming (class method)
[Java] Basic method notes
Easy way to check method / field list in Java REPL
[Java] New Thread generation method (2)
Java GC method determination conditions
Java Silver Study Method Memo
Create a java method [Memo] [java11]
Object-oriented (java) with Strike Gundam
Java test code method collection
Object-oriented summary by beginners (Java)
[Java Silver] About equals method
Java 3 major elements (object-oriented) memorandum
Main functions of scope method
[Java] Timer processing implementation method
[Java] Random number generation method (Random)
Java methods and method overloads
Benefits of Java static method
[Java Silver] Array generation method
[Java] New Thread generation method (1)