You can set access for fields and methods in your Android app. A Java class can __have either public access or nonpublic (default) access. But a member of a class has four possibilities: public, private, default, and protected.
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.
To see these rules in action, check out the public class in this code.
package com.allyourcode.bank;
public class Account {
public String customerName;
private int internalIdNumber;
String address;
String phone;
public int socialSecurityNumber;
int accountType;
double balance;
public 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
UseAccoun
t class is in the same package as theAccount
class. - The
UseAccount
class can create a variable of typeAccount
. - The
UseAccount
class’s code can refer to the publiccustomerName
field of theAccount
class and to the defaultaddress
field of theAccount
class. - The
UseAccount
class cannot refer to the privateinternalIdNumber
field of theAccount
class, even thoughUseAccount
andAccount
are in the same package. - The
UseAccountFromOutside
class is not in the same package as theAccount
class. - The
UseAccountFromOutside
class can create a variable of typeAccount
. (Animport
declaration keeps you from having to repeat the fully qualifiedcom.allyourcode.bank.Account
name everywhere in the code.) - The
UseAccountFromOutside
class’s code can refer to the publiccustomerName
field of theAccount
class. - The
UseAccountFromOutside
class’s code cannot refer to the defaultaddress
field of theAccount
class or to the privateinternalIdNumber
field of theAccount
class.
Now examine the nonpublic class in this code.
package com.allyourcode.game;
class Sprite {
public String name;
String image;
double distanceFromLeftEdge, distanceFromTop;
double motionAcross, motionDown;
private 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
UseSprite
class is in the same package as theSprite
class. - The
UseSprite
class can create a variable of typeSprite
. - The
UseSprite
class’s code can refer to the publicname
field of theSprite
class and to the defaultdistanceFromTop
field of theSprite
class. - The
UseSprite
class cannot refer to the privaterenderingValue
field of theSprite
class, even thoughUseSprite
andSprite
are in the same package. - The
UseSpriteFromOutside
class isn’t in the same package as theSprite
class. - The
UseSpriteFromOutside
class cannot create a variable of typeSprite
. (Not even animport
declaration can save you from an error message here.) - Inside the
UseAccountFromOutside
class, references tosprite.name, sprite.distanceFromTop
, andsprite.renderingValue
are all meaningless because thesprite
variable doesn’t __have a type.