I know we can nest a class to access its private fields and methods, like the following example, which compiles fine:
public class Outer {
Inner inner = new Inner();
public class Inner {
private Inner() {
}
}
}
However, what I want to achieve is sort of like this:
public class A {
B b = new B();
}
class B {
private B() { // error: B() has private access in B
}
}
I know in C++, we can use the friend
keyword, but I've learned there is no friend or friend equivalent in Java. So how do I achieve that?
Update #1
: The two code snippets above are not in one Java file. They are just two separate examples. Sorry for the confusion.
Update #2
: This problem was solved by using packages.