Menu
Coddy logo textTech

Fields (Attributes)

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 6 of 87.

Fields (attributes) are variables that store data inside a class. They are usually private and accessed through getter and setter methods.

Declaring fields

public class Person {
    private String name;
    private int age;
}

Adding getters and setters

public class Person {
    private String name;
    private int age;
    
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return this.age;
    }
    
    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        }
    }
}

Using the class

Person person = new Person();
person.setName("Alice");
person.setAge(25);
System.out.println(person.getName());  // Alice

Making fields private is called encapsulation. It protects data by controlling access through methods, allowing you to add validation in setters.

challenge icon

Challenge

Medium

Create a BankAccount class with private fields and getter methods:

  • Private fields: accountName (String) and balance (double)
  • A constructor to set both fields
  • Getter methods for each field
  • deposit() method to add money
  • withdraw() method that returns "Success" or "Insufficient funds"

Cheat sheet

Fields (attributes) are variables that store data inside a class. They are usually private and accessed through getter and setter methods.

Declaring private fields:

public class Person {
    private String name;
    private int age;
}

Adding getters and setters:

public class Person {
    private String name;
    private int age;
    
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return this.age;
    }
    
    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        }
    }
}

Using the class:

Person person = new Person();
person.setName("Alice");
person.setAge(25);
System.out.println(person.getName());  // Alice

Making fields private is called encapsulation. It protects data by controlling access through methods, allowing you to add validation in setters.

Try it yourself

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        double initial = Double.parseDouble(scanner.nextLine());
        double deposit = Double.parseDouble(scanner.nextLine());
        
        BankAccount account = new BankAccount(name, initial);
        
        System.out.println("Account: " + account.getAccountName());
        System.out.println("Balance: " + account.getBalance());
        
        account.deposit(deposit);
        System.out.println("After deposit: " + account.getBalance());
        
        String result = account.withdraw(2000);
        System.out.println("Withdraw 2000: " + result);
        System.out.println("Final Balance: " + account.getBalance());
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming