JAVA constructor call processing

Thank you. Below, I will write an article for my memo.

First, create Main.java for the main process

Main.java


class Main {
    public static void main(String[] args) {
        //Instance creation and assignment to variables
        Sample sample = new Sample();

        System.out.println("【sample】");
        System.out.println("name:" + sample.name);
    }
}

Next, create Sample.java that calls the constructor

Sample.java


class Sample {
    //Definition of constants
    public String name;

    //Constructor definition
    Sample() {
        this.name = "Let's go";
    }
}

On the 4th line of Main.java

Sample sample = new Sample();

Execute the constructor of Sample.java (lines 6-8 of Sample.java).

Then on line 6 of Main.java

System.out.println("name:" + sample.name);

In this process, it was set by the constructor of Sample.java

Outputs the name (with "Let's" set in the contents) to the console of the processing result.

The result of compiling and running the two java files is ...

【sample】
Name: Let's go

It will be.

I feel like I was able to call the sample.java constant as private ... Please point out if there is any better way to write it.

Excuse me.

Recommended Posts

JAVA constructor call processing
java (constructor)
Java constructor
[Java] Multi-thread processing
Understand java constructor
[Java] Stream processing
java iterative processing
Call Java from JRuby
Java random, various processing
Call a program written in Swift from Processing (Java)
[Java] Multi-thread processing --Exclusive control
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Timer processing implementation method
Measured parallel processing in Java
Understanding Java Concurrent Processing (Introduction)
Summary of java error processing
I studied the constructor (java)
[Kotlin] Get Java Constructor / Method from KFunction and call it
Call Kotlin's sealed class from Java
[For beginners] Summary of java constructor
Date processing in Java (LocalDate: Initialization)
Delegate some Java processing to JavaScript
[Java] Loop processing and multiplication table
Run node.js from android java (processing)
[Processing × Java] How to use variables
Java
Java overload constructor starting from beginner
Server processing with Java (Introduction part.1)
Surprisingly deep Java list inversion-Stream processing
Basic processing flow of java Stream
About file copy processing in Java
Notes on Android (java) thread processing
Java
Call TensorFlow Java API from Scala
Deleting files using recursive processing [Java]
[Processing × Java] How to use arrays
[Java] Processing time measurement method memo
constructor
[Java] Exception types and basic processing
Call the super method in Java
[Java] Pass arguments to constructor in Mockito / Set method default call to callRealMethod
Data processing using stream API from Java 8
[Processing x Java] Construction of development environment
Call GitHub API from Java Socket API part2
Call Java method from JavaScript executed in Java
Call the Windows Notification API in Java
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
Why does Java call a file a class?
[Processing × Java] How to use the class
Call Java methods from Nim using jnim
Force non-instantiation with Effective Java private constructor
[Processing × Java] How to use the function
[Java ~ Conditional branching / Iterative processing ~] Study memo (3)
Call Visual Recognition in Watson Java SDK
[Java] Instance method, instance field, class method, class field, constructor summary