Java Fields and Methods

In the last lesson you learned about Java creating objects. An object on its own is just an empty box. What goes inside it is two kinds of members:

  • Fields remember data.
  • Methods do work.

🤔 Why a class needs both

Think about a bank account. It is two things at once:

  • A balance it holds. That is data, sitting still.
  • Actions like deposit and withdraw. Those are jobs that change the data.

Data alone is a lifeless record. Actions alone have nothing to act on. So you need both. The data is the field. The action is the method.

This program shows the two side by side.

class BankAccount {
double balance; // field: the data it remembers
void deposit(double amount) { // method: an action it can do
balance = balance + amount;
}
}

The field balance is what the account knows. The method deposit is what it does. One line stores state, the other changes it.

📦 Fields store state

A field is a variable that lives inside an object:

  • It is also called an instance variable, since every object gets its own copy.
  • It holds the object’s state, the data it remembers right now.

Let’s define a class with two fields and read them.

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.owner = "Alex"; // set a field
account.balance = 100.0; // set another field
System.out.println(account.owner); // read a field
System.out.println(account.balance);
}
}
class BankAccount {
String owner; // field
double balance; // field
}

Walk through it:

  • The class declares two fields, owner and balance, what every account remembers.
  • new BankAccount() builds an object with its own owner and balance.
  • account.owner = "Alex" puts a value into that object’s owner field.
  • account.owner and account.balance then read the values back.

Fields are declared once in the class but get real values per object. The class says an account has an owner and a balance. The object is where “Alex” and 100.0 actually live.

Output

Alex
100.0

⚙️ Methods define behavior

A method is a named block of code that belongs to the class:

  • It is the object’s behavior, the work it can do.
  • It can take inputs, run steps, and give a result back.
  • The new idea here: a method can read and change the object’s own fields.

Here is an account that can deposit money. The method changes a field.

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.balance = 100.0;
account.deposit(50.0); // run the deposit behavior
System.out.println(account.balance);
}
}
class BankAccount {
double balance;
void deposit(double amount) {
balance = balance + amount; // change the object's own field
}
}

Let’s read it:

  • deposit is a method that takes one input, amount.
  • Inside it, the bare word balance means “the balance field of this object”.
  • So balance = balance + amount reads the balance, adds the amount, and stores it back.
  • After account.deposit(50.0), the balance goes from 100.0 to 150.0.

The method does not need the object name in front of balance. It already knows which object it belongs to. We will see why next.

Output

150.0

🔗 The dot operator reaches members

To use a field or method from outside, you use the dot operator:

  • The dot means “the member belonging to this object”.
  • Object goes on the left, member on the right.

This program uses the dot for fields and methods, on two objects.

public class Main {
public static void main(String[] args) {
BankAccount alexAccount = new BankAccount();
alexAccount.owner = "Alex"; // dot reaches a field
alexAccount.balance = 100.0;
BankAccount riyaAccount = new BankAccount();
riyaAccount.owner = "Riya";
riyaAccount.balance = 500.0;
alexAccount.deposit(50.0); // dot reaches a method
riyaAccount.deposit(200.0);
alexAccount.show();
riyaAccount.show();
}
}
class BankAccount {
String owner;
double balance;
void deposit(double amount) {
balance = balance + amount;
}
void show() {
System.out.println(owner + " has " + balance);
}
}

Here is what the dot does each time:

  • alexAccount.owner is “the owner field of Alex’s account”.
  • alexAccount.deposit(50.0) is “run deposit on Alex’s account”.
  • When deposit runs for Alex, balance inside it means Alex’s balance.
  • When deposit runs for Riya, the same code means Riya’s balance.

The method is written once but acts on whichever object sits before the dot. So the two accounts never mix, because each object carries its own fields.

Output

Alex has 150.0
Riya has 700.0

🧮 A fuller worked class

Let’s put it together with a withdraw that refuses to go below zero. Read the whole program first.

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.owner = "Alex";
account.balance = 100.0;
account.deposit(50.0); // balance becomes 150.0
account.withdraw(30.0); // balance becomes 120.0
account.withdraw(500.0); // refused, not enough money
account.show();
}
}
class BankAccount {
String owner;
double balance;
void deposit(double amount) {
balance = balance + amount;
}
void withdraw(double amount) {
if (amount > balance) {
System.out.println("Not enough money.");
return; // stop here, change nothing
}
balance = balance - amount;
}
void show() {
System.out.println(owner + " has " + balance);
}
}

Now the walkthrough:

  • The fields owner and balance hold the state. Everything else acts on them.
  • deposit reads balance, adds to it, and stores it back.
  • withdraw checks first. If the amount is bigger than balance, it prints a message and returns early, so the balance stays the same.
  • If there is enough money, withdraw subtracts from balance.
  • The last withdraw(500.0) is refused and never touches the balance.

The methods guard the field. Because the method decides the rules, the data stays safe. That is the real value of joining data and behavior.

Output

Not enough money.
Alex has 120.0

🏛️ Instance members vs static members

Almost everything above is an instance member:

  • It belongs to each object, so each account has its own balance.
  • You need an object to use it.

There is a second kind, the static member:

  • It belongs to the class itself, not to any one object.
  • There is one copy, shared by everybody, used through the class name.

This program shows the difference. The count is shared. The id belongs to each object.

public class Main {
public static void main(String[] args) {
Robot r1 = new Robot();
Robot r2 = new Robot();
System.out.println(r1.id); // each object has its own id
System.out.println(r2.id);
System.out.println(Robot.count); // one shared count for the class
}
}
class Robot {
static int count = 0; // static: one copy for the whole class
int id; // instance: one copy per object
Robot() {
count = count + 1; // bump the shared counter
id = count; // give this object its own number
}
}

How they are used:

  • r1.id reaches id through an object, because each robot has its own.
  • Robot.count reaches count through the class, because there is only one.
  • Each new robot bumps the shared count, then copies that number into its own id.

Output

1
2
2

Static gets its own lesson

For now: an instance member belongs to each object and needs an object to use it. A static member belongs to the class and is shared by all objects. We cover static fully in a later lesson.

🔒 A peek at getters and setters

Right now anyone can write account.balance = -999 from outside, skipping our withdraw guard. The fix:

  • Make the field private so outside code cannot touch it directly.
  • Add a getter, a method that reads the field.
  • Add a setter, a method that changes the field with a check.

Here is the same account with a getter and a setter for the balance.

class BankAccount {
private double balance; // hidden from outside
public double getBalance() { // getter: reads the field
return balance;
}
public void setBalance(double amount) { // setter: changes the field with a check
if (amount >= 0) {
balance = amount;
}
}
}

To read the balance you call getBalance(). To change it you call setBalance(...), which checks the value first. The full idea, called encapsulation, gets its own lesson. For now, just know getters and setters are ordinary methods that guard a field.

⚠️ Common Mistakes

A few slip-ups to watch for.

  • Accessing an instance member without an object. An instance field or method needs an object before the dot, not the class name.
// ❌ balance is an instance field, the class has no balance of its own
BankAccount.balance = 100.0;
// ✅ create an object, then use the dot on it
BankAccount account = new BankAccount();
account.balance = 100.0;
  • Shadowing a field with a local variable. A local variable with the same name as a field wins inside that method, so the field never changes.
class BankAccount {
double balance;
void deposit(double amount) {
// ❌ this makes a NEW local balance and updates that, not the field
double balance = this.balance + amount;
}
}
class BankAccount {
double balance;
void deposit(double amount) {
// ✅ no local variable, so balance means the field
balance = balance + amount;
}
}
  • Forgetting that each object has its own fields. Changing r1.id does not change r2.id. Instance fields are per object, not shared.

  • Ignoring what a method actually does. Don’t expect deposit to return a value when it returns nothing. Check what the method does before you rely on it.

✅ Best Practices

Good habits for fields and methods.

  • Noun-like field names. balance, owner, id, what the object remembers.
  • Verb-like method names. deposit, withdraw, show, what the action does.
  • Let methods guard the fields. Put the rules inside the method, like checking the amount before a withdraw, so the data stays correct.
  • Keep a field and the methods that use it in the same class. Behavior belongs next to the data it changes.
  • Use the field name directly inside its own methods. Avoid a local variable with the same name, so you do not shadow the field by accident.

🧩 What You’ve Learned

Nice work. The two members that make up every class:

  • ✅ A class has two kinds of members: fields that store state and methods that define behavior.
  • ✅ A field is a variable inside an object, holding the data that object remembers.
  • ✅ A method is a named action that can read and change the object’s own fields.
  • ✅ You reach a field or a method with the dot operator, like account.balance or account.deposit(50.0).
  • ✅ Inside a method, a bare field name means “the field of this object”, so the same method works on each object’s own data.
  • ✅ An instance member belongs to each object; a static member belongs to the class and is shared.
  • ✅ Getters and setters are ordinary methods that read and guard a field, a first step toward encapsulation.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does a field store in a class?

    Why: A field is a variable inside the object that holds its state, the data it remembers.

  2. 2

    Inside a method, what does the bare word balance refer to?

    Why: A bare field name inside a method means the field of the object the method is acting on.

  3. 3

    How do you reach an instance field from outside the object?

    Why: Instance members are reached with the dot operator on an object, like account.balance.

  4. 4

    What is true about a static member?

    Why: A static member belongs to the class itself and there is one shared copy for all objects.

🚀 What’s Next?

Fields are variables that live inside an object. But methods also use temporary variables that live only while the method runs. Those are local variables, and they behave differently. They appear, do their job, and vanish. Let’s learn how they work and why the difference matters.

Java Local Variables

Share & Connect