Programming with Java: Access for Fields and Methods in Your Android App - dummies

Programming with Java: Access for Fields and Methods in Your Android App

By Barry A. Burd

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, or protected) 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.

java-programming-for-android-developers-2e-public-class
Referring to a public class in the same package.

Let’s see what happens with a different package.

java-programming-for-android-developers-2e-public-class-different-package
Referring to a public class in 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 the Account class.
  • The UseAccount class can create a variable of type Account.
  • The UseAccount class’s code can refer to the public customerName field of the Account class and to the default address field of the Account class.
  • The UseAccount class cannot refer to the private internalIdNumber field of the Account class, even though UseAccount and Account are in the same package.
  • The UseAccountFromOutside class is not in the same package as the Account class.
  • The UseAccountFromOutside class can create a variable of type Account. (An import declaration keeps you from having to repeat the fully qualified com.allyourcode.bank.Account name everywhere in the code.)
  • The UseAccountFromOutside class’s code can refer to the public customerName field of the Account class.
  • The UseAccountFromOutside class’s code cannot refer to the default address field of the Account class or to the private internalIdNumber field of the Account 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.

java-programming-for-android-developers-2e-default-access-class
Referring to a default access class in the same package.

Let’s see what happens with a different package.

java-programming-for-android-developers-2e-default-access-class-different-package
Referring to a default access class in 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 the Sprite class.
  • The UseSprite class can create a variable of type Sprite.
  • The UseSprite class’s code can refer to the public name field of the Sprite class and to the default distanceFromTop field of the Sprite class.
  • The UseSprite class cannot refer to the private renderingValue field of the Sprite class, even though UseSprite and Sprite are in the same package.
  • The UseSpriteFromOutside class isn’t in the same package as the Sprite class.
  • The UseSpriteFromOutside class cannot create a variable of type Sprite. (Not even an import declaration can save you from an error message here.)
  • Inside the UseAccountFromOutside class, references to sprite.name, sprite.distanceFromTop, and sprite.renderingValue are all meaningless because the sprite variable doesn’t have a type.