BAEL-1210 Guide to the Static Keyword in Java (#2767)
* Adding Source Files * Guide to the Static Keyword in Java - Test files
This commit is contained in:
parent
f5a6fc886e
commit
1d5b17ad6c
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.staticdemo;
|
||||
|
||||
/**
|
||||
* This class demonstrates the use of static fields and static methods
|
||||
* the instance variables engine and displacement are distinct for
|
||||
* each and every object whereas static/class variable numberOfCars
|
||||
* is unique and is shared across all objects of this class.
|
||||
*
|
||||
* @author baeldung
|
||||
*
|
||||
*/
|
||||
public class Car {
|
||||
private String name;
|
||||
private String engine;
|
||||
|
||||
public static int numberOfCars;
|
||||
|
||||
public Car(String name, String engine) {
|
||||
this.name = name;
|
||||
this.engine = engine;
|
||||
numberOfCars++;
|
||||
}
|
||||
|
||||
//getters and setters
|
||||
public static int getNumberOfCars() {
|
||||
return numberOfCars;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public void setEngine(String engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public static void setNumberOfCars(int numberOfCars) {
|
||||
Car.numberOfCars = numberOfCars;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.staticdemo;
|
||||
|
||||
public class Singleton {
|
||||
private Singleton() {}
|
||||
|
||||
private static class SingletonHolder {
|
||||
public static final Singleton instance = new Singleton();
|
||||
}
|
||||
|
||||
public static Singleton getInstance() {
|
||||
return SingletonHolder.instance;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.staticdemo;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class StaticBlock {
|
||||
private static List<String> ranks = new LinkedList<>();
|
||||
|
||||
static {
|
||||
ranks.add("Lieutenant");
|
||||
ranks.add("Captain");
|
||||
ranks.add("Major");
|
||||
}
|
||||
|
||||
static {
|
||||
ranks.add("Colonel");
|
||||
ranks.add("General");
|
||||
}
|
||||
|
||||
//getters and setters
|
||||
public static List<String> getRanks() {
|
||||
return ranks;
|
||||
}
|
||||
|
||||
public static void setRanks(List<String> ranks) {
|
||||
StaticBlock.ranks = ranks;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.staticdemo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CarIntegrationTest {
|
||||
@Test
|
||||
public void whenNumberOfCarObjectsInitialized_thenStaticCounterIncreases() {
|
||||
new Car("Jaguar", "V8");
|
||||
new Car("Bugatti", "W16");
|
||||
assertEquals(2, Car.numberOfCars);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.staticdemo;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SingletonIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenStaticInnerClass_whenMultipleTimesInstanceCalled_thenOnlyOneTimeInitialized() {
|
||||
Singleton object1 = Singleton.getInstance();
|
||||
Singleton object2 = Singleton.getInstance();
|
||||
|
||||
Assert.assertSame(object1, object2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.staticdemo;
|
||||
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StaticBlockIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() {
|
||||
List<String> actualList = StaticBlock.getRanks();
|
||||
assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue