Final Static Variables in Java

This commit is contained in:
michaelin007 2023-12-03 07:19:26 +00:00
parent dea22a6863
commit 692f3b201a
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.finalstatic;
import java.util.HashMap;
public class Bike {
public static final int TIRE = 2;
public static final int PEDAL;
public static final HashMap<String, Integer> PART = new HashMap<>();
static {
PEDAL = 5;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.finalstatic;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class BikeUnitTest {
@Test
void givenTireConstantSetUponDeclaration_whenGetTire_thenReturnTwo() {
assertEquals(2, Bike.TIRE);
}
@Test
void givenPedalConstantSetByStaticBlock_whenGetPedal_thenReturnFive() {
assertEquals(5, Bike.PEDAL);
}
@Test
void givenPartConstantObject_whenObjectStateChanged_thenCorrect() {
Bike.PART.put("seat", 1);
assertEquals(1, Bike.PART.get("seat"));
Bike.PART.put("seat", 5);
assertEquals(5, Bike.PART.get("seat"));
}
}