From a75cc221814fc2e4dde7b5ccda98e5760e4abbd8 Mon Sep 17 00:00:00 2001 From: Thomas Duxbury Date: Tue, 1 Jun 2021 19:41:51 +0100 Subject: [PATCH] =?UTF-8?q?=C2=96BAEL-4986:=20Private=20constructors=20in?= =?UTF-8?q?=20Java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../privateconstructors/Employee.java | 39 +++++++++++++++++++ .../PrivateConstructorClass.java | 8 ++++ .../privateconstructors/SingletonClass.java | 20 ++++++++++ .../privateconstructors/StringUtils.java | 16 ++++++++ .../privateconstructors/ValueTypeClass.java | 22 +++++++++++ 5 files changed, 105 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/Employee.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/PrivateConstructorClass.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/SingletonClass.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/StringUtils.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/ValueTypeClass.java diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/Employee.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/Employee.java new file mode 100644 index 0000000000..96785b0fb0 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/Employee.java @@ -0,0 +1,39 @@ +package com.baeldung.privateconstructors; + +public class Employee { + + private final String name; + private final int age; + private final String department; + + private Employee(String name, int age, String department) { + this.name = name; + this.age = age; + this.department = department; + } + + public static class Builder { + private String name; + private int age; + private String department; + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setAge(int age) { + this.age = age; + return this; + } + + public Builder setDepartment(String department) { + this.department = department; + return this; + } + + public Employee build() { + return new Employee(name, age, department); + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/PrivateConstructorClass.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/PrivateConstructorClass.java new file mode 100644 index 0000000000..904b626f7d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/PrivateConstructorClass.java @@ -0,0 +1,8 @@ +package com.baeldung.privateconstructors; + +public class PrivateConstructorClass { + + private PrivateConstructorClass() { + System.out.println("In the private constructor"); + } +} diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/SingletonClass.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/SingletonClass.java new file mode 100644 index 0000000000..a02ef37f7e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/SingletonClass.java @@ -0,0 +1,20 @@ +package com.baeldung.privateconstructors; + +public final class SingletonClass { + + private static SingletonClass INSTANCE; + private String info = "Initial info class"; + + private SingletonClass() { + } + + public static SingletonClass getInstance() { + if (INSTANCE == null) { + INSTANCE = new SingletonClass(); + } + + return INSTANCE; + } + + // getters and setters +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/StringUtils.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/StringUtils.java new file mode 100644 index 0000000000..8422b524fd --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/StringUtils.java @@ -0,0 +1,16 @@ +package com.baeldung.privateconstructors; + +public class StringUtils { + + private StringUtils() { + System.out.println("This class cannot be instantiated"); + } + + public static String toUpperCase(String s) { + return s.toUpperCase(); + } + + public static String toLowerCase(String s) { + return s.toLowerCase(); + } +} diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/ValueTypeClass.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/ValueTypeClass.java new file mode 100644 index 0000000000..ff449ea9e9 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/privateconstructors/ValueTypeClass.java @@ -0,0 +1,22 @@ +package com.baeldung.privateconstructors; + +public class ValueTypeClass { + + private final String value; + private final String type; + + public ValueTypeClass(int x) { + this(Integer.toString(x), "int"); + } + + public ValueTypeClass(boolean x) { + this(Boolean.toString(x), "boolean"); + } + + private ValueTypeClass(String value, String type) { + this.value = value; + this.type = type; + } + + // getters and setters +}