A class's fields and methods are the class's members.
Here's how member access works:- A default member of a class (a member whose declaration doesn't contain the words
public,private, orprotected) can be used by any code inside the same package as that class. - A private member of a class cannot be used in any code outside the class.
- A public member of a class can be used wherever the class itself can be used; that is:
- Any program in any package can refer to a public member of a public class.
- For a program to reference a public member of a default access class, the program must be inside the same package as the class.
<strong>package com.allyourcode.bank;</strong>
<strong>public</strong> class Account {
<strong>public</strong> String customerName;
<strong>private</strong> int internalIdNumber;
String address;
String phone;
<strong>public</strong> int socialSecurityNumber;
int accountType;
double balance;
<strong>public</strong> static int findById(int internalIdNumber) {
Account foundAccount = new Account();
// Code to find the account goes here.
return foundAccount.internalIdNumber;
}
}
The code uses the Account class and its fields.
Let’s see what happens with a different package.
The error messages point to some troubles with the code. Here's a list of facts about these two pieces of code:
- The
UseAccount class is in the same package as theAccountclass. - The
UseAccountclass can create a variable of typeAccount. - The
UseAccountclass's code can refer to the publiccustomerNamefield of theAccountclass and to the defaultaddressfield of theAccountclass. - The
UseAccountclass cannot refer to the privateinternalIdNumberfield of theAccountclass, even thoughUseAccountandAccountare in the same package. - The
UseAccountFromOutsideclass is not in the same package as theAccountclass. - The
UseAccountFromOutsideclass can create a variable of typeAccount. (Animportdeclaration keeps you from having to repeat the fully qualifiedcom.allyourcode.bank.Accountname everywhere in the code.) - The
UseAccountFromOutsideclass's code can refer to the publiccustomerNamefield of theAccountclass. - The
UseAccountFromOutsideclass's code cannot refer to the defaultaddressfield of theAccountclass or to the privateinternalIdNumberfield of theAccountclass.
<strong>package com.allyourcode.game;</strong>
class Sprite {
<strong>public</strong> String name;
String image;
double distanceFromLeftEdge, distanceFromTop;
double motionAcross, motionDown;
<strong>private</strong> int renderingValue;
void render() {
if (renderingValue == 2) {
// Do stuff here
}
}
}
The code uses the Sprite class and its fields.
Let’s see what happens with a different package.
The error messages in these images point to some troubles with the code. Here's a list of facts about these two pieces of code:
- The
UseSpriteclass is in the same package as theSpriteclass. - The
UseSpriteclass can create a variable of typeSprite. - The
UseSpriteclass's code can refer to the publicnamefield of theSpriteclass and to the defaultdistanceFromTopfield of theSpriteclass. - The
UseSpriteclass cannot refer to the privaterenderingValuefield of theSpriteclass, even thoughUseSpriteandSpriteare in the same package. - The
UseSpriteFromOutsideclass isn’t in the same package as theSpriteclass. - The
UseSpriteFromOutsideclass cannot create a variable of typeSprite. (Not even animportdeclaration can save you from an error message here.) - Inside the
UseAccountFromOutsideclass, references tosprite.name, sprite.distanceFromTop, andsprite.renderingValueare all meaningless because thespritevariable doesn't have a type.






