Making Private Fields Accessible to Other Classes - Java Object Oriented Design

Java examples for Object Oriented Design:Field

Introduction

Encapsulate the private fields by making getters and setters to access them.

Demo Code

public class Player {
    /*  www .  j a  v a2s .  c  o  m*/
    private String firstName = null;
    private String lastName = null;
    private String position = null;
    private int status = -1;
    
    public Player(){
        
    }
    
    public Player(String position, int status){
        this.position = position;
        this.status = status;
    }
    
    protected String playerStatus(){
        String returnValue = null;
        
        switch(getStatus()){
                case 0:
                        returnValue = "ACTIVE";
                case 1:
                        returnValue = "INACTIVE";
                case 2:
                        returnValue = "INJURY";
                default:
                        returnValue = "ON_BENCH";
        }
        
        return returnValue;
    }
    
    public String playerString(){
        return getFirstName() + " " + getLastName() + " - " + getPosition();
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the position
     */
    public String getPosition() {
        return position;
    }

    /**
     * @param position the position to set
     */
    public void setPosition(String position) {
        this.position = position;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(int status) {
        this.status = status;
    }
}

Related Tutorials