I am trying to test the following situation:
city.Main)package castle)castle.Guard), and a package-private class (castle.Princess).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!
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?
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.)
Princess class public and use it .package castle;
// added public modifier
public class Princess {
public void sayHi() { System.out.println("Hi world"); }
}
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();
}
}
You are not going to be able to return a type that is privately scoped within a class. How can any external sources know what that type even IS if it's privately scope inside of another class?
This design won't even compile.
Edit:
The simple solution is to make it public and then you can use it externally outside of the package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With