Abekus — AI learning and job matching platform logo

Fields and methods

Lesson 2 of 84 in OOPs Interview Mastery. Free to read, no account needed.

What fields and methods are

A class is a blueprint for objects, and it describes two things: the data each object carries and the actions each object can perform. The data is held in fields, and the actions are written as methods. Think of a shopping cart in a grocery app: it has an item count and a running total, and it can add an item.

class Cart {
    int itemCount;
    long totalPaise;
}

This Cart class declares two fields. Each cart you create from it gets its own copy of both fields.

Instance fields hold state

An instance field is a variable declared inside a class, with a type and a name, that holds the state of one particular object. Every object created from the class gets its own separate copy of each field. You read or write a field with dot notation: the object name, a dot, then the field name.

Cart a = new Cart();
Cart b = new Cart();
a.itemCount = 2;

After the last line, a.itemCount reads 2, while b.itemCount still reads 0. Changing one cart's field did not touch the other cart.

Default values of fields

If you create an object and never assign a value to one of its fields, Java fills the field with a default value based on its type. Numeric fields become 0, boolean fields become false, and reference-type fields such as String become null, which means the field points to no object at all. Consider this class, created with no assignments.

FieldTypeValue before any assignment
quantityint0
paiselong0
giftbooleanfalse
skuStringnull

So for Item i = new Item();, the expression i.gift gives false straight away, without you assigning anything.

Declaring instance methods

An instance method is a named block of code inside a class that works on one object. Its declaration has a return type, a name, a parameter list in round brackets, and a body in curly braces. The parameters are variables that receive values when the method is called, and those values are the arguments supplied at the call site. A method whose return type is a real type, such as long, must send a value back with a return statement.

long lineTotal(int quantity, long unitPricePaise) {
    return quantity * unitPricePaise;
}

With a cart c, the call c.lineTotal(2, 2800L) sends 2 and 2800 in as arguments and returns 5600. A method declared void returns nothing, so it has no return value.

Methods that change fields

A method can read and modify the fields of the object it belongs to. Here, addItem is a void method that adds to the running total and the item count stored in the cart's fields. It uses the field names directly, with no dot needed, because it is already inside the object.

void addItem(int quantity, long unitPricePaise) {
    totalPaise = totalPaise + quantity * unitPricePaise;
    itemCount = itemCount + quantity;
}

Start with a new cart, where both fields are 0. Then:

  1. Calling cart.addItem(2, 2800) makes totalPaise 5600 and itemCount 2.
  2. Calling cart.addItem(1, 4500) makes totalPaise 10100 and itemCount 3.

Each call changes the same cart's fields, so the second call builds on the first.

Lessons in Objects and Classes Basics

  1. Class vs object
  2. Fields and methods
  3. Constructor invocation
  4. Reference variable vs object
  5. Private fields with getters
  6. Public vs private vs protected
  7. Package-private default access
  8. == vs equals
  9. Object identity vs equality
  10. Equals and hashcode contract

84 lessons across 9 topics. See the full course