Java class make field private

Description

Java class make field private



class Account {/*  w ww  .  ja v  a2  s  .  com*/
    private String name;
    private String address;
    private double balance;

    public void setName(String n) {
        name = n;
    }

    public String getName() {
        return name;
    }

    public void setAddress(String a) {
        address = a;
    }

    public String getAddress() {
        return address;
    }

    public void setBalance(double b) {
        balance = b;
    }

    public double getBalance() {
        return balance;
    }
}

public class Main {

    public static void main(String args[]) {
        Account myAccount = new Account();
        Account yourAccount = new Account();

        myAccount.setName("Java");
        myAccount.setAddress("222 Main Lane");
        myAccount.setBalance(24.02);

        yourAccount.setName("Javasctipt");
        yourAccount.setAddress("111 Main Street");
        yourAccount.setBalance(55.63);

        System.out.print(myAccount.getName());
        System.out.print(" (");
        System.out.print(myAccount.getAddress());
        System.out.print(") has $");
        System.out.print(myAccount.getBalance());
        System.out.println();

        System.out.print(yourAccount.getName());
        System.out.print(" (");
        System.out.print(yourAccount.getAddress());
        System.out.print(") has $");
        System.out.print(yourAccount.getBalance());
    }
}



PreviousNext

Related