[Java beginner] I got a little deeper understanding of "It's time to use new", so make a note

When reading the Java source User user = new User(); Anyone can see the description. Yes, instance creation.

I came to understand this, but when I actually touched the program, I was confused by the following description.

check.java



public boolean userExists(String userId) {

	//Use User type variable
	User user = select.selectOne(userId);

	if (user == null) {
		return false;
	}

	return true;
}

It was suddenly described as ʻUser user` without new. For me, who had vaguely thought, "When I use another class, I'm new," I don't understand why.

But that was simply my misunderstanding.

Looking at the contents of the above method select.selectOne (userId);

select.java


public User selectOne(String userId) {
	
	//Get 1 data
	Map<String, Object> map = jdbc.queryForMap("SELECT * FROM m_user "
		+ "WHERE user_id = ?"
		, userId);

	//Variable for returning results
	User user = new User();

	//Set the acquired data in user
	user.setUserId((String) map.get("user_id"));		//User ID
	user.setUserName((String) map.get("user_name"));	//User name

	//Return user instance
	return user;
}

It has become. Yes, I firmly do ʻUser user = new User ();` and new, and return that user instance as the return value. In other words, since the User instance has already been created, there was no need to create a new one in the first check.java class.

If you ask me, I wouldn't know if I created a new instance each time. ..

That's why I got a little deeper understanding of the phrase, "The timing to new is (only) when you use it." maybe.

In the Command pattern, create a command in advance and execute the created command when needed. Just like installing (preparing) commands in advance on Linux etc. and executing commands when needed.

Thank you for pointing out. Java is still deep, isn't it? I had never heard of the Command pattern, so I looked it up, but I couldn't figure it out just by looking at it. I will study design patterns in the future. Thank you very much for your good opportunity.

Recommended Posts

[Java beginner] I got a little deeper understanding of "It's time to use new", so make a note
How to leave a comment
I passed the Java test level 2 so I will leave a note
[Java beginner] I got a little deeper understanding of "It's time to use new", so make a note
It's good to go to a programming school.
I was a little addicted to the S3 Checksum comparison, so I made a note.
[JAVA] Project Euler, I got stuck in Q8, so make a note
I tried to make a client of RESAS-API in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I was a little addicted to the S3 Checksum comparison, so I made a note.
[Small story] I tried to make the java ArrayList a little more convenient
[Java] I studied polymorphism, so I will summarize how to use it and its merits.
I tried to make a new sorting algorithm, but I don't know if it's really new
Since I use it often, I make a note of DB related things on Android.
I read the readable code, so make a note
I want to use a little icon in Rails
I tried to make a login function in Java
kintone clone? I was quite addicted to launching OSS WebDB Extension with Lightsail + Docker, so make a note of it.
A note that I had trouble when trying to use nginx with Remote-Containers of vscode
[Beginner] Try to make a simple RPG game with Java ①
[Beginner] I made a program to sell cakes in Java
I want to make a specific model of ActiveRecord ReadOnly
I want to make a list with kotlin and java!
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
I passed the Java test level 2 so I will leave a note
[Java] I tried to make a maze by the digging method ♪
It's almost the end of the year, so it's a story of cleaning up to use Docker comfortably at the beginning of the year
I'm worried about producing with JAVA and XML, so make a note of what I'm trying to do without forgetting (display in for statement)
How to make a Java container
How to make a Java array
A little understanding of lambda expressions
I was addicted to a simple test of Jedis (Java-> Redis library)
Java beginner tried to make a simple web application using Spring Boot
[Java] How to use substring to cut out a part of a character string
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
I tried to make full use of the CPU core in Ruby
I tried to make a talk application in Java using AI "A3RT"
I translated the grammar of R and Java [Updated from time to time]