[BAEL-3149] Java 'private' access modifier (#7622)
* Added private access modifier to tutorials * added private access modifier example to core-lang-2 module * [BAEL-3149] Java 'private' access modifier * Update core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/core/modifiers/Employee.java Co-Authored-By: KevinGilmore <kpg102@gmail.com> * Update core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/core/modifiers/Employee.java Co-Authored-By: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
parent
1888614f84
commit
3a5f594754
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String privateId;
|
||||
public String name;
|
||||
private boolean manager;
|
||||
|
||||
public Employee(String id, String name) {
|
||||
changeId(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private Employee(String id, String name, boolean managerAttribute) {
|
||||
this.privateId = id;
|
||||
this.name = name;
|
||||
this.privateId = id + "_ID-MANAGER";
|
||||
}
|
||||
|
||||
public void changeId(String customId) {
|
||||
if (customId.endsWith("_ID")) {
|
||||
this.privateId = customId;
|
||||
} else {
|
||||
this.privateId = customId + "_ID";
|
||||
}
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return privateId;
|
||||
}
|
||||
|
||||
public boolean isManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public void elevateToManager() {
|
||||
if ("Carl".equals(this.name)) {
|
||||
setManager(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setManager(boolean manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public static Employee buildManager(String id, String name) {
|
||||
return new Employee(id, name, true);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class ExampleClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Employee employee = new Employee("Bob","ABC123");
|
||||
employee.changeId("BCD234");
|
||||
System.out.println(employee.getId());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class PublicOuterClass {
|
||||
|
||||
public PrivateInnerClass getInnerClassInstance() {
|
||||
PrivateInnerClass myPrivateClassInstance = this.new PrivateInnerClass();
|
||||
myPrivateClassInstance.id = "ID1";
|
||||
myPrivateClassInstance.name = "Bob";
|
||||
return myPrivateClassInstance;
|
||||
}
|
||||
|
||||
private class PrivateInnerClass {
|
||||
public String name;
|
||||
public String id;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue