What about public method returns private class instance?
class, java, package, private, public
Solution
`Princess` class is default scoped to `castle` package , so its invisible within `city` package . To circumvent that :
(We can do one of the following three approaches.)
1) Make `Princess` class `public` and use it .
package castle;
// added public modifier
public class Princess {
public void sayHi() { System.out.println("Hi world"); }
}
2) Or, Define a `public interface` and let `Princess` implement it and use the `interface reference` instead of the `class reference` . I would prefer this .
castle \ IPrincess.java
// Interface definition
package castle;
public interface IPrincess {
public void sayHi();
}
castle \ Princess.java
// Implement the interface in Princess class
package castle;
class Princess implements IPrincess {
public void sayHi() { System.out.println("Hi world"); }
}
castle \ Guard.java
// Modify the Guard class
package castle;
public class Guard {
public IPrincess getPrincess() {
return new Princess();
}
public static IPrincess getPrincessStatic() {
return new Princess();
}
// for here i use Princess instead of IPrincess. (@midnite)
private Princess m_princess = new Princess();
public IPrincess getPrincessMember() {
return m_princess;
}
}
city \ Main.java
// Modify the main class
package city;
import castle.Guard;
import castle.IPrincess;
public class Main {
public static void main(String[] args) {
IPrincess princess = Guard.getPrincessStatic();
Guard.getPrincessStatic().sayHi();
Guard guard = new Guard();
guard.getPrincess().sayHi();
guard.getPrincessMember().sayHi();
}
}
3) Or, Put all the classes in same package .
Problem
I am trying to test the following situation: - There is a main. (`city.Main`) - There is a package. (`package castle`) - Within the package, there is a public class (`castle.Guard`), and a package-private class (`castle.Princess`). - What if, the public class is returning an instance of the private class? Here is my code: Main.java ``` package city; import castle.Guard; public class Main { public static void main(String[] args) { Princess princess = Guard.getPrincessStatic(); // Error: Princess cannot be resolved to a type Guard.getPrincessStatic().sayHi(); // Error: The type Princess is not visible Guard guard = new Guard(); guard.getPrincess().sayHi(); // Error: The type Princess is not visible guard.getPrincessMember().sayHi(); // Error: The type Princess is not visible } } ``` Guard.java ``` package castle; public class Guard { public Princess getPrincess() { return new Princess(); } public static Princess getPrincessStatic() { return new Princess(); } private Princess m_princess = new Princess(); public Princess getPrincessMember() { return m_princess; } } ``` Princess.java ``` package castle; class Princess { public void sayHi() { System.out.println("Hi world"); } } ``` Notice all the 4 statements in `main()` are having errors. I have done some research too. In fact i want to mimic this answer. But i don't why my codes throw errors. Thanks for any explanations! Edit: I intend to make the `castle-Princess` package-private. I know that, by returning a package-private class out of its package, I should be prepared for errors. But why that answer works, while mine doesn't?