What is the constructor for?

What is a constructor?

A special method that runs automatically when an instance is created ** You can freely control the initial state of the instance! ** **

That said, let's actually execute the code.

What if there is no constructor?

** Default for each type is entered in the value ** For example, suppose you have a program that retrieves customer information.

	//Define customer information
	class CustomerCard {
		int id; //Customer id
		String name; //Customer name
	}

	class CustomerManager {
		public static void main(String[] args) {
			CustomerCard out = new CustomerCard(); //Instance creation
			System.out.println(out.id); //Output customer id
			System.out.println(out.name); //Output customer name
		}
	}

When I do this, the result is:

0 //Customer id
null //Customer name

Why this happens

When a class is instantiated (new), member variables that do not contain any value are automatically initialized. Remember that there is a rule that numeric types such as int are initialized to 0, boolean types are initialized to false, and reference types such as String are initialized to null.

Try to put in the constructor

Let's put the constructor in the previous program, set the ** initial state **, and execute it.

	class CustomerCard {
		int id; //Customer id
		String name; //Customer name

	//**Set the initial value in the constructor**
	CustomerCard() {
			this.id = 1111;
			this.name = "taylor";
		}
	}

	class CustomerManager {
		public static void main(String[] args) {
			CustomerCard out = new CustomerCard(); //Instance creation
			System.out.println(out.id); //Output customer id
			System.out.println(out.name); //Output customer name
		}
	}

The result is this.

1111 //Customer id
taylor //Customer name

Yeah, the value specified in the constructor is the initial value. It would be nice to understand roughly that the constructor can set the initial value of the instance like this.

Recommended Posts

What is the constructor for?
What is a constructor?
What is the Facade pattern useful for?
What is the volatile modifier for Java variables?
What is the pluck method?
[Ruby] What is `!!` used for?
What is the initialize method?
'% 02d' What is the percentage of% 2?
What is Cubby
What is Docker?
What is null? ]
Spring Autowired is written in the constructor
What is the right migration data type? ??
What is Keycloak
What is testing? ・ About the importance of testing
What is maven?
What is Jackson?
[For programming beginners] What is a method?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is the best file reading (Java)
What is params
What is SLF4J?
[For super super beginners] What is object orientation?
What is Facade? ??
What is the model test code testing
What is Java <>?
What is Docker? What purpose is it used for?
What is Gradle?
What is POJO
What is the main method in Java?
What is Java
What is the data structure of ActionText?
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Pullback doing in The Composable Architecture
What is JSP? ~ Let's know the basics of JSP !! ~
What is the Java Servlet / JSP MVC model?
What is different from the PHP language. [Note]
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is Maven Assembly?
What is `docker-compose up`?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?