2

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.

  • 1
    when you work with `package`s you could make the `B` constructor `package-private` (So just remove the `private`-modifier) – Lino May 23 '18 at 20:24
  • It's best to avoid declaring more than one top-level class per file. – shmosel May 23 '18 at 20:47
  • 1
    Private means private. What you are asking is how can I make a private member not be private. Surely the only answer can be "delete the private keyword". – Boris the Spider May 23 '18 at 20:47
  • @shmosel Thanks for pointing that out. Actually, I didn't put them in one file. They are just two seperate examples. I will update my original post. – OneFlowerOneWorld May 23 '18 at 20:53
  • You can create a non private static method in class B that can return a new instance of B to A – Joseph Peter May 23 '18 at 22:11
  • @JosephPeter In that case, every class can have an instance of class B. However, I only want class A to have instances of class B. – OneFlowerOneWorld May 24 '18 at 04:47
  • @Lino I have thought about that. But, if I put classes A and B in one package, and write my application in another package but import class B, will I have access to class B's constructor? – OneFlowerOneWorld May 24 '18 at 04:53
  • @JosephPeter And I don't want to enclose class B in class A because it will make class A hefty when code gets long. – OneFlowerOneWorld May 24 '18 at 04:55
  • @Lino I just experimented on that and it worked, aulthough it requires extra work. – OneFlowerOneWorld May 24 '18 at 05:12
  • @OneFlowerOneWorld to your first comment, no you will have not. class `B` will only be available to that package. And to the latter: it actually does not. Java Applications are meant to be split up. And to do that the most efficient way you have to use classes in different packages. So its not really extra work, but just a habit – Lino May 24 '18 at 06:07
  • @OneFlowerOneWorld Also you might want to post a self answer explaining what you did and what actually solved your problem. E.g. with an additional code snippet. Don't just say it in your question – Lino May 24 '18 at 06:12

2 Answers2

-1

You need to setup accessor methods to be able to access the private methods in the other class file.

Ashish Patel
  • 163
  • 6
-2

You can use the static keyword while defining B. Without static, as you have it, B is an inner class; with static, B is a nested class. Nested classes cannot access the private instance fields of the enclosing class, as inner classes can.

You can also change the scope of the B() constructor to default, so that A can see it. However, in my experience, having two top-level types in one file is not done.

Steve11235
  • 2,849
  • 1
  • 17
  • 18