Java class definition and instantiation

Class definition and instance generation

Use "new class name ()" to instantiate a class. [Example]

class Main {
  public static void main(String[] args) {
  new Person();  //Person(This time people)Create an instance of the class
  }
}

Person.java


class Person{
}

Instance to variable

To assign an instance to a variable, use "class type variable name = new class name ()". When assigning an instance, specify the class type, and the class name becomes the class type as it is. [Example]

class Main {
  public static void main(String[] args) {
  Person person = new Person();
  }
}

Person.java


class Person{
}

Instance method

The instance method uses "public return type method name ()".

Main.java


class Main {
  public static void main(String[] args) {
    Person person = new Person();  //void is the return type
    person.hello();
  }
}

Person.java


class Person {
  public void hello() {
  System.out.println("Good morning");
  }
}

Instance field definition

For the instance field definition, add public before the variable definition, such as "public data type variable name". [Example]

Person.java


class Person {
  public String name;
}

String name defines the definition to put the name. For the instance field, use "instance name.field name". [Example]

Main.java


~abridgement~
    Person person = new Person();
    person.name("Sato");
    System.out.println(person.name);  //person.Getting the value of name with name
~abridgement~

[Example]

Main.java


class Main {
  public static void main(String[] args) {
    Person person = new Person();
    person.hello();
    person.name = "Sato";
    System.out.println(person.name); 
    }
}

Person.java


class Person {
  public String name;
  public void hello() {
    System.out.println("Good morning");
  }
}

this Use the "this" variable to access the instance field in the method. this can only be used in method definitions within a class. This is replaced with the instance calling the method when it is called. [Example]

Main.java


class Main {
  public static void main(String[] args) {
    Person person = new Person();
    person.hello();  //person calls this
    person.name = "Sato";
    System.out.println(person.name); 
    }
}

Person.java


class Person {
  public String name;
  public void hello() {
    System.out.println("Good morning" + this.name);  //this.(This time)name field of hello method
  }
}

Recommended Posts

Java class definition and instantiation
StringBuffer and StringBuilder Class in Java
Java class methods
Class and model
[Java] Class inheritance
java Scanner class
Java HashMap class
java (abstract class)
[Java] Differences between instance variables and class variables
[Java] Inheritance and structure of HttpServlet class
[Java] Nested class
Java anonymous class
About Java class
Java and JavaScript
XXE and Java
Java programming (static clauses and "class variables")
[java] abstract class
[Java] Object class
Java local class
Java constant definition
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[Java] How to use Calendar class and Date class
About class division (Java)
Getters and setters (Java)
About Java StringBuilder class
[Java] Thread and Runnable
Java true and false
[Java] About Singleton Class
[Java] String comparison and && and ||
abstract (abstract class) and interface (interface)
Java inner class review
Java class type field
Java programming (class method)
About Java String class
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
Class and instant part2
Java programming (class structure)
[Java] Variables and types
java (classes and instances)
About java abstract class
[Java] Overload and override
Write a class in Kotlin and call it in Java
Use of Abstract Class and Interface properly in Java
[Java] Handling of character strings (String class and StringBuilder class)
[Java Silver] What are class variables instance variables and local variables?
Study Java # 2 (\ mark and operator)
[Java] Integer wrapper class reference
Java memo (standard class) substring
Implement Thread in Java and try using anonymous class, lambda
java.util.Optional class and null and not null
Find the address class and address type from the IP address with Java
[Java] Difference between == and equals
[Java] Stack area and static area
Java inflexible String class substring
[Java] Generics classes and generics methods
Java encryption and decryption PDF
What is the LocalDateTime class? [Java beginner] -Date and time class-
Java and Iterator Part 1 External Iterator