Simon
Simon

Reputation: 14472

Does the private keyword in a class level field declaration actually do anything?

I'm refactoring a bunch of classes, written by different people, which do not have consistent standards. During the course of this, I came to ponder on whether there is any difference between these declarations:

public class foo(){

    int fooBar = 1;

    ......

and

public class foo(){

    private int fooBar = 1;

    ......

Please note that this is a question of semantics, I am fully aware of scoping, encapsulation etc. The question is, does using the private scope annotation in a class field do anything?

Thanks for all thoughts..

(PS. My current level of understanding says there is no difference)

Upvotes: 2

Views: 295

Answers (1)

Adam Mihalcin
Adam Mihalcin

Reputation: 14478

Absolutely there is a difference. The default access level for a class member in Java is package-private, not private. This means that in the first version any class in the same package as Foo can access fooBar, while in the second version this is not the case.

Upvotes: 13

Related Questions