[Java] Make it a constant

Assuming that you are writing the source and make variables that you use many times. Let's make constants for "fixed" "character strings" that are used many times. [Constants are variables whose values cannot be changed. It becomes a constant by specifying the final modifier]

Reason: If a correction is made later, you only have to fix one place. Because the readability of the source increases I don't want to change the value I don't want to change

Method of constantization: Declare the final constant first, even if you assign a value when you declare it. You can assign the value later, but you can't change the value after you assign it.

Declare a constant again [Variable name is ... all uppercase] (Use _ (underscore) to reconnect multiple words)

String inputWords = "aiueo";

⇒ final String INPUT_WORDS = "aiueo";

⇒ static final String INPUT_WORDS = "aiueo";

Memo: If you define a constant, memory will be allocated. Cannot be included in for (it will be called many times)

Constants are defined outside the method (main is the method ☆)

By adding static, you can write to the VM and call it from there. It doesn't use much memory, so let's define it with static final ♪

Only fixed ones are made constant. Don't change the value!

Recommended Posts

[Java] Make it a constant
Make a rhombus using Java
Priority Queue max Make it a queue
How to make a Java array
How to make a Java calendar Summary
How to make a Discord bot (Java)
[Java] Let's make a DB access library!
Java constant definition
I did Java to make (a == 1 && a == 2 && a == 3) always true
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Create Scala Seq from Java, make Scala Seq a Java List
Make Blackjack in Java
[Java] Create a filter
java build a triangle
Make a reflection utility ②
Make a reflection utility ③
Make a reflection utility ①
Run Scala with GraalVM & make it a native image
Make "I'm not a robot" in Java EE (Jakarta EE)
Write a class in Kotlin and call it in Java
[Java basics] Let's make a triangle with a for statement
[Personal memo] Make a simple deep copy in Java
Let's make a robot! "A simple demo of Java AWT Robot"
I tried to make a login function in Java
[Java] Make it a constant
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
A note about Java GC
[Summary] Why make it object-oriented?
Java --How to make JTable
[Beginner] Try to make a simple RPG game with Java ①
Refactoring: Make Blackjack in Java
Create a java method [Memo] [java11]
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!
[Java] Create a temporary file
Find a subset in Java
What is a Java collection?
[Java] Draw a simple pattern
Java Language Feature and How it Produced a Subtle Bug
[Rails] Make a breadcrumb trail
I tried to make a client of RESAS-API in Java
Java creates a Word document
[Java] I tried to make a maze by the digging method ♪
How to make a groundbreaking diamond using Java for statement wwww
Think of a Java update strategy
Build a Java project with Gradle
Make a language! (Making a simple calculator ②)
Try to make a simple callback
3 Implement a simple interpreter in Java
Reading Memo "Why make it object-oriented"
Make a slideshow tool with JavaFX
I created a PDF in Java.
I made a shopify app @java
Upload a file using Java HttpURLConnection
A person writing C ++ tried writing Java
Make a Christmas tree with swift
How to make a JDBC driver
How slow is a Java Scanner?
Run a batch file from Java
Make your own persistence FW (Java)