How to use java class

Yesterday about three terms I wrote, but today about the actual usage I will write.

Basic

To check the basic usage of the class, create a program with the following contents.

Member variables

Inside the "{" and "}" that define the class That is, it is described in curly braces And it is a variable described outside the method. Also called an instance variable.

Member variables


void nowTime(){
    System.out.println("Current" + time + "is");
  }

Code example

TestClass.java


class TestClass {
    //Member variables
    public String name = "Samurai";
    
    //Method
    public String testMethod(){
        return "Engineer";
    }
}

Main.java


public class Main {
 
    public static void main(String[] args) {
        //Create an object of class
        TestClass c = new TestClass();
        
        //Refer to member variables
        String str1 = c.name;
        
        //Method call
        String str2 = c.testMethod();
        
        //Output result
        System.out.println(str1 + str2);
    } 
}

as a result

Output result


> javac Main.java
> java Main
SamuraiEngineer

easy explanation

Basic flow To explain in detail

TestClass.java


    //① Set member variables
    public String name = "Samurai";
    // 

TestClass.java


    //(2) Define the method and return Enginerr.
    public String testMethod(){
        return "Engineer";
    }
}

Main.java


        //③TestClass.Create class object from java
        TestClass c = new TestClass();

Main.java


        //(4) Refer to the member variable, which is set in (2).
        String str1 = c.name;

Main.java


        //⑤ Call the method of ② that has been set
        String str2 = c.testMethod();

Main.java


        //(6) Outputs the result set in the variable.
        System.out.println(str1 + str2);

Recommended Posts

How to use java class
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
[Java] How to use the File class
[Java] How to use the HashMap class
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
How to use java Optional
[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 ①
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Calendar class and Date class
How to use Java HttpClient (Get)
How to disassemble Java class files
How to use Java HttpClient (Post)
How to use the wrapper class
[Processing × Java] How to use variables
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
How to use Java enum type
[Java] How to use compareTo method of Date class
How to use Map
How to use rbenv
[Java] How to use the hasNext function
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use submit method (Java Silver)
How to use collection_select
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
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 Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to use JUnit 5
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]