[Java] Make variables in extended for statement and for Each statement immutable

When declared normally

If you declare the extended for statement and for Each statement normally, it will be as follows. Since it is not declared final, it can be reassigned (normal for statements are skipped because reassignment is essential in principle).

List<Integer> intList = Arrays.asList(1, 2, 3, 4);

for(int i : intList) {
    i = 0; //Can be rewritten
}

intList.forEach(it -> {
    it = 0; //Can be rewritten
});

In this article, we will rewrite these contents into a non-reassignable form.

Make the variable immutable

For extended for statement

You can make it ʻimmutable by declaring it with final`.

for(final int i : intList) {
    i = 0; //Since it is declared final, it cannot be reassigned and a compile error occurs.
}

For foreach statement

You can make the argument final by specifying the type in parentheses as shown below.

intList.forEach((final int it) ->
    it = 0; //Since it is declared final, it cannot be reassigned and a compile error occurs.
);

Another solution

For the time being, the argument can be final even when implementing it as an anonymous class.

Consumer<Integer> finalConsumer = new Consumer<Integer>() {
    @Override
    public void accept(final Integer it) {
        it = 0; //Since it is declared final, it cannot be reassigned and a compile error occurs.
    }
};

apology

When this article was first published, the title and content was "** Indexes can be immutable only for extended for statements, extended for statements, and forEach statements **", but @ saka1029 pointed out. As you can see, this was an error, so I completely rewrote both the title and content.

Thank you for pointing out @ saka1029.

Recommended Posts

[Java] Make variables in extended for statement and for Each statement immutable
[Java] for Each and sorted in Lambda
[Java] for statement / extended for statement
I tried using an extended for statement in Java
How to loop Java Map (for Each / extended for statement)
Difference between final and Immutable in Java
Java for statement
Compare the speed of the for statement and the extended for statement.
Enable / disable SNI in Java for each communication
Make Blackjack in Java
(Memo) Java for statement
hash and each statement
[Java] Variables and types
[Java basics] Let's make a triangle with a for statement
[Java] Difference between static final and final in member variables
Use variables for class and id names in haml
This and that for editing ini in Java. : inieditor-java
[Beginner] Java variable / logical operator / extended for statement [Note 22]
[For beginners] Explanation of classes, instances, and statics in Java
About for statement and if statement
[Java] Get Map key / value pairs using extended for statement
[Java] Basic statement for beginners
Java programming (variables and data)
Refactoring: Make Blackjack in Java
☾ Java / Iterative statement and iterative control statement
Make your own simple server in Java and understand HTTP
StringUtils.isNotBlank is convenient for empty judgment and null judgment in java
Define abstract methods in Java enum and write each behavior
Tips for using Salesforce SOAP and Bulk API in Java
Java while and for statements
Processing time measurement for each BCrypt + stretch count in Java
Differences in default behavior of memory and CPU recognition on kubernetes (GKE) for each Java version
Utilization of Java array elements, for, length, value, and extended for statements
How to make a groundbreaking diamond using Java for statement wwww
AWS SDK for Java 1.11.x and 2.x
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
Java Primer Series (Variables and Types)
Encoding and Decoding example in Java
[For beginners] Run Selenium in Java
Java for beginners, expressions and operators 2
StringBuffer and StringBuilder Class in Java
Understanding equals and hashCode in Java
How to name variables in Java
Settings for SSL debugging in Java
Classes and instances Java for beginners
Hello world in Java and Gradle
[JDBC] I tried to make SQLite3 database access from Java into a method for each SQL statement.
Comparison of processing speed between Stream including cast and extended for statement
[Java] How to turn a two-dimensional array with an extended for statement
Automatically transfer animations for each title and store them in a folder
[Java] Nowadays, the extended for statement is not exclusively for List, isn't it?
[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
Java starting from beginner, variables and types
Easy to make Slack Bot in Java
Make bubble sort and selection sort in Ruby
First steps for deep learning in Java
Key points for introducing gRPC in Java
[For beginners] Difference between Java and Kotlin
[Java] Differences between instance variables and class variables
must not return in the for statement