I noticed in my Java book, in the section about packages and the private modifier, that the code redundantly used private
on the class and the members of the class being accessed outside of the package.
package bookpack;
public class Book {
private String title;
private String author;
private int pubDate;
public Book(String t, String a, int d) {
title = t;
author = a;
pubDate = d;
}
public void show() {
System.out.println(title);
System.out.println(author);
System.out.println(pubDate + "\n");
}
}
When I remove the public
from show()
, Eclipse gives an error stating that the member cannot be accessed (when attempting to do so from another package). I understand that it is because it is not public
and therefore cannot be accessed from outside the package. However, since the class is public, I thought that all members of the class would then be public
, unless otherwise specified. That would follow the "general specifications here, specific specifications later" style, similar to inheritance. Much like how you cannot call a dynamic object from a static method. So why is the public
tag required on the member of a public class? How does a public
tag affect accessibility in the context of retrieving a public member of a class