0Pricing
Java Academy · Lesson

Fields, Methods, Constructors

Learn how to add fields for data, methods for behavior, and constructors for easy object setup.

Fields, Methods, Constructors is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Fields

Fields: variables inside a class that hold data. Each object has its own copy of the fields.

Methods

Methods: functions inside a class that describe behavior. They can use and update fields.

Constructors

Constructors: special methods called when you create objects. They set up initial field values.

Why constructors?

Constructors make object creation simple and reduce code repetition. You pass values once when you create the object.

Steps

Step 1: Define fields. Step 2: Write a constructor to set them. Step 3: Add a method that uses the fields. Step 4: Create and use the object.

Demo code

Code: Dog has fields, a constructor, and a bark method. Each object is set up with name and age when created.

public class Main {
  // Simple class with fields, constructor, and method
  static class Dog {
    String name;
    int age;

    // Constructor sets fields when object is created
    Dog(String n, int a) {
      name = n;
      age = a;
    }

    // Method uses fields
    void bark() {
      System.out.println(name + " says: Woof!");
    }
  }

  public static void main(String[] args) {
    // Create Dog objects using constructor
    Dog d1 = new Dog("Buddy", 3);
    Dog d2 = new Dog("Luna", 5);

    // Call method on each
    d1.bark();
    d2.bark();
  }
}

Constructor check

Quick check: What does a constructor do in Java?

Recap

Recap: Fields hold data, methods add behavior, and constructors set up objects at creation.

Frequently asked questions

Is the “Fields, Methods, Constructors” lesson free?

Yes — the full text of “Fields, Methods, Constructors” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fields, Methods, Constructors”?

Learn how to add fields for data, methods for behavior, and constructors for easy object setup. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Fields, Methods, Constructors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Classes & Objects
  2. Fields, Methods, Constructors
  3. Encapsulation (getters/setters)
← Back to Java Academy