-[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.
--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.
--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.
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.
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. ↓ ↓
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. ↓ ↓
-[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