Hi,
I've passed this task but I don't understand pretty well the why of it. Could someone help me with some explanation?
How does the private modifier on the getName method affect the result?
Thanks
This is the task
package com.codegym.task.task23.task2310;
/*
Strain your brain!
*/
public class Solution {
private String name;
Solution(String name) {
this.name = name;
}
private String getName() {
return name;
}
private void sout() {
new Solution("sout") {
void printName() {
System.out.println(getName());
}
}.printName();
}
public static void main(String[] args) {
new Solution("main").sout();
}
}
An this is the solution.
package com.codegym.task.task23.task2310;
/*
Strain your brain!
*/
public class Solution {
private String name;
Solution(String name) {
this.name = name;
}
String getName() {
return name;
}
private void sout() {
new Solution("sout") {
void printName() {
System.out.println(getName());
}
}.printName();
}
public static void main(String[] args) {
new Solution("main").sout();
}
}