[JAVA-621] core-java-lang-oop-modifiers module
* Creation * Moved code from https://www.baeldung.com/java-static * Moved code from https://www.baeldung.com/java-final * Moved code from https://www.baeldung.com/java-public-keyword * Moved code from https://www.baeldung.com/java-access-modifiers * Moved code from https://www.baeldung.com/java-private-keyword * Moved code from https://www.baeldung.com/java-static-default-methods * Moved code from https://www.baeldung.com/java-strictfp * Moved article references to the new README.md
This commit is contained in:
@@ -4,13 +4,11 @@ This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to hashCode() in Java](https://www.baeldung.com/java-hashcode)
|
||||
- [A Guide to the Static Keyword in Java](https://www.baeldung.com/java-static)
|
||||
- [Polymorphism in Java](https://www.baeldung.com/java-polymorphism)
|
||||
- [Method Overloading and Overriding in Java](https://www.baeldung.com/java-method-overload-override)
|
||||
- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy)
|
||||
- [Guide to Inheritance in Java](https://www.baeldung.com/java-inheritance)
|
||||
- [Object Type Casting in Java](https://www.baeldung.com/java-type-casting)
|
||||
- [The “final” Keyword in Java](https://www.baeldung.com/java-final)
|
||||
- [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure)
|
||||
- [Variable and Method Hiding in Java](https://www.baeldung.com/java-variable-method-hiding)
|
||||
- [Object-Oriented-Programming Concepts in Java](https://www.baeldung.com/java-oop)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.finalkeyword;
|
||||
|
||||
public class BlackCat {
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.finalkeyword;
|
||||
|
||||
public class BlackDog extends Dog {
|
||||
// public void sound() {
|
||||
// }
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.finalkeyword;
|
||||
|
||||
public final class Cat {
|
||||
|
||||
private int weight;
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public void methodWithFinalArguments(final int x) {
|
||||
// x=1;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.finalkeyword;
|
||||
|
||||
public class Dog {
|
||||
|
||||
public final void sound() {
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.finalkeyword;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class FinalUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenChangedFinalClassProperties_thenChanged() {
|
||||
Cat cat = new Cat();
|
||||
cat.setWeight(1);
|
||||
|
||||
assertEquals(1, cat.getWeight());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFinalVariableAssign_thenOnlyOnce() {
|
||||
final int i;
|
||||
i = 1;
|
||||
// i=2;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangedFinalReference_thenChanged() {
|
||||
|
||||
final Cat cat = new Cat();
|
||||
// cat=new Cat();
|
||||
cat.setWeight(5);
|
||||
|
||||
assertEquals(5, cat.getWeight());
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user