From ae96297bc2c1ea2610bfc9bd8175b3ba9e828ee9 Mon Sep 17 00:00:00 2001 From: tudormarc <93496164+tudormarc@users.noreply.github.com> Date: Fri, 7 Oct 2022 17:57:45 +0300 Subject: [PATCH] BAEL-5675 - variable initialization on declaration vs on constructor (#12800) * BAEL-5675 - variable initialization on declaration vs on constructor * moved classes under a different project --- .../com/baeldung/variableInitialization/A.java | 13 +++++++++++++ .../com/baeldung/variableInitialization/B.java | 4 ++++ .../variableinitialization/AUnitTest.java | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/A.java create mode 100644 core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/B.java create mode 100644 core-java-modules/core-java-lang-5/src/test/java/com/baeldung/variableinitialization/AUnitTest.java diff --git a/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/A.java b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/A.java new file mode 100644 index 0000000000..f0045253e1 --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/A.java @@ -0,0 +1,13 @@ +package com.baeldung.variableInitialization; + +public class A { + private B b = new B(); + + public A() { + this.b = new B(); + } + + public A(B b) { + this.b = b; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/B.java b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/B.java new file mode 100644 index 0000000000..7d92541db6 --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/variableInitialization/B.java @@ -0,0 +1,4 @@ +package com.baeldung.variableInitialization; + +public class B { +} diff --git a/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/variableinitialization/AUnitTest.java b/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/variableinitialization/AUnitTest.java new file mode 100644 index 0000000000..529196f300 --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/variableinitialization/AUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.variableinitialization; + +import static org.mockito.Mockito.mock; + +import org.junit.jupiter.api.Test; + +import com.baeldung.variableInitialization.A; +import com.baeldung.variableInitialization.B; + +public class AUnitTest { + + @Test + public void whenCreatingTest_useDependencyInjection() { + // given + B b = mock(B.class); + A a = new A(b); + } +}