Why do I have to do a "method"? ~ "Main only" would have worked ~

problem

It went well up to the main method. However, it suddenly became difficult after the appearance of "methods" and "objects".

スクリーンショット 2020-01-11 13.30.56.png

Conclusion

By making it an object, you can call variables and methods set in your favorite file at your favorite timing. By making it a method, it becomes possible to skip the saved or processed data as a return value and receive it as an argument. With these, you can freely pass data to any file.

I will explain what that means.

Scope of problem occurrence

1. Call the "Human" file from the "Main1" file.

Main1.java


public class Main1 {
	public static void main(String[] args) {

		Human human = new Human();
		human.name = "Between";
		human.hello();

	}
}

2. The contents of the "Human" file.

Human.java


public class Human {
	String name;
	
	public void hello(){
		System.out.println(name +"Hi,");
	}	
}

3. The result of the terminal.

Aida's, Hello

Question

Isn't it necessary to prepare two files? → Is it possible to make it only with main?

1. Make the same result with only the "Main2" file.

Main2.java


public class Main2 {

	public static void main(String[] args) {
		String name;
		
		name = "Between";
		System.out.println(name +"Hi,");
	}
}

2. It's done.

Aida's, Hello

Conclusion 1

I found that I didn't need any objects or methods in the following cases:

--One data (name) to save --One data processing (output using name)

New question

Earlier there was only one "Aida-san", What happens if "Ueda-san" is also added?

1. Add "Ueda" to the "Main2" file.

Main2.java


public class Main2 {

	public static void main(String[] args) {
		String name;
		name = "Between";
		//■ ① Add "Ueda".
		name = "Ueda";
		
		//■ "Hello" for the ② "Between" Mr. intact
		System.out.println(name +"Hi,");
		//■ add the "Hello" for the ③ "Ueda" Mr.
		System.out.println(name +"Hi,");
	}
}

2. I couldn't.

Mr. Ueda, Hello
Mr. Ueda, Hello

Conclusion 2

If you try to do it only with the main method,

--The same variable (variable is overwritten) cannot be used. --Since variables cannot be used, data processing does not work well. (Only "Ueda" appears.)

solution

Increase the variable.

Main2.java


public class Main2 {

	public static void main(String[] args) {
		String name1;
		String name2;
		name1 = "Between";
		name2 = "Ueda";
		
		System.out.println(name1 +"Hi,");
		System.out.println(name2 +"Hi,");
	}
}

Conclusion 3

When there are two or more data you want to save,

--You have to prepare two variables. --You have to think about variable names. ――When you look at it later, you may not know which variable contains "which data".

Summary

It is better to separate by object than to process only by main method I found the following.

--Data can be saved with the same variable name. --You can retrieve data at any time you like.

Reflections

In the next post, "Summary" With the part that "data can be retrieved at any time" Be able to explain the benefits of using "methods".

Recommended Posts

Why do I have to do a "method"? ~ "Main only" would have worked ~
[Ruby] I want to do a method jump!
I want to call a method of another class
I made a method to ask for Premium Friday
I want to call the main method using reflection
I want to call a method and count the number
I'm glad to have a method to blink characters on Android
How to create a method
I made a method to ask for Premium Friday (Java 8 version)
[Java] I tried to make a maze by the digging method ♪
I tried to explain the method