[Easy-to-understand explanation! ] How to use Java instance

1. Prior knowledge

-[Latest] How to build Java environment on Ubuntu

Or

-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)

As prior knowledge, the contents of the above link are required.

2. What is an instance?

--The entity (object) created based on the class is called instance. --Creating an object based on a class is called instantiation. -- Instance can be created by using new operator from a class when calling (using) a variable or method of another class. --The constructor is called when the instance is created.

3. What is a constructor?

--Constructor is a method that is executed when a class is instantiated, and is mainly used when initializing member variables of that class. --If there is no description of the constructor, default constructor is called. --The default constructor is a constructor that is empty (no processing) with no arguments.

4. Basic writing

Basic writing


public class main class name{
    public static void main(String[] args) {
        //Instance generation
Class name Variable name=new class name();    
    }
}
 
class class name{
    //Constructor (executed when instantiating)
name of the class(){
Initialization process, etc.
    }
}

--Basic instance creation is described as above.

5. Description example

Test.java


//Instance and constructor test classes
public class Test {
	//main method
	public static void main(String[] args) {
	    //instance(object)Generate a(Constructor call)
		Hello hello = new Hello();
	}
}

//Instance test class
class Hello {
  //constructor
  Hello() {
	  //Hello display
	  System.out.print("Hello");
  }
}

--Copy the above statement, specify S-JIS as the character code, save the file name as Test.java, and execute it at the command prompt. ↓ ↓ 01.png

6. Special description example

Test1.java


//Instantiation in constructor
public class Test1 {
	//main method
	public static void main(String[] args) {
	    //instance(object)Generate a(Constructor call)
		new Hello1();
	}
}

//Constructor test class
class Hello1 {
  //constructor
  Hello1() {
      //instance(object)Generate a
      InstanceHello ih = new InstanceHello();
      //Instance method invocation
      ih.showHello();
  }
}

//Instance test class
class InstanceHello {
  //Hello display method
  void showHello(){
      System.out.print("Hello");
  }
}

Test2.java


//Instances not stored in variables
public class Test2 {
    //main method
    public static void main(String[] args) {
        //instance(object)Generate a&Method call
    	new Hello2().showHello();
    }
}

//Instance test class
class Hello2 {
  //Hello display method
  void showHello(){
      //Hello display
      System.out.print("Hello");
  }
}

--Copy the above statement, specify S-JIS as the character code, save the file name as Test1.java, Test2.java, and execute it at the command prompt. ↓ ↓ 02.png

7. Related

-[Latest] How to build Java environment on Ubuntu -[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1) -[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)

Recommended Posts

[Easy-to-understand explanation! ] How to use Java instance
[Easy-to-understand explanation! ] How to use Java polymorphism
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java encapsulation
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java classes, definitions, import
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
try-catch-finally exception handling How to use java
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables