exporting non_public type through public API
I have written this code in Netbeans but it will show this warning for the name of this method ,would you please help me for what it shows this warn开发者_JAVA技巧ing? thanks
public Node returnNode(int index) throws IndexOutOfBoundsException {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
} else {
for (int i = 0; i < index; i++) {
pointer = pointer.getNext();
}
}
return pointer;
}
The non-public type is your Node
class in this case. Most likely you just want to declare it as public class Node
instead of just class Node
.
Netbeans seems to be quite focused on large projects. In a scenario where you are writing a library package that will be used by external code, this warning makes a lot of sense. If you are writing a small standalone app, there is little harm in setting the visibility of all your classes to public
.
That warning is understandable. A public method that returns a non-public type is going to be hard to use. For a start, unless the return type is visible to the caller, it won't be able to assign it to anything.
You should probably change the visibility of either the method or the return type.
Your public method returns a Node, from the error I would guess that the Node class is not declared public and can't be seen by the same classes as your method.
So instead of
public Node returnNode(int index)
those classes will only see
public Object returnNode(int index)
To remove the warning make the Node class public.
精彩评论