Merge pull request #15350 from Michaelin007/finalstatic
Final Static Variables in Java
This commit is contained in:
commit
a4f17e64f4
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.staticfinal;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.staticfinal;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMathClass_whenAccessingPiConstant_thenVerifyPiValueIsCorrect() {
|
||||
assertEquals(3.141592653589793, Math.PI);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue