Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 2

Writing your first class

Fields, methods, and why they are not static

18m45 XP
→→→

What you'll be able to do

  • Declare a class with fields and instance methods
  • Explain why an instance method must not be static
  • Read and write the Student example from memory
  • Predict what a field holds before anyone assigns it
1

Declaring a Class

Declaring a class

A class declaration creates a brand new type that you can use exactly like int or String.

The Student class, one line at a time

Main.java
1class Student {
2 String name;
3 int age;
4
5 void introduce() {
6 System.out.println("My name is " + name);
7 }
8}
class Student {
class is the keyword, Student is the new type name (PascalCase). Everything between the braces is a member of the class.
String name;
A field. No value is given here because the class is only a blueprint — each object will hold its own name.
int age;
A second field. Fields are declared directly inside the class body, never inside a method.
void introduce() {
An instance method: no static, because it needs a specific student to introduce. void because it prints instead of returning.
System.out.println("My name is " + name);
name with no prefix means *this object's* name. Java silently reads the field of whichever object the method was called on.
}
Closes the method, then the class. Indentation is style; braces are the real structure.

Why is `Main` public but `Student` is not?

A source file may contain several classes but only one public one, and its name must match the file name. In this course you write helper classes without public next to public class Main, exactly as you would in a single-file exercise.

Designing before typing

Before writing a class, answer two questions in plain language: *what does it know?* (fields) and *what can it do?* (methods). 'A book knows its title, author and page count; it can describe itself' becomes three fields and one method — no guessing required.

Prove it — coding challenges

2

Fields

Fields hold the object's state

Field vs local variable — a distinction beginners miss

Field (instance variable)Local variable
Declaredinside the class bodyinside a method
Livesas long as the object livesuntil the method returns
Default value0 / 0.0 / false / nullnone — you must assign before use
Visible toevery method of the objectonly its own method
Purposeremember statetemporary calculation

A field you forget to set is not empty — it is null or 0

new Student() then introduce() prints My name is null. Nothing crashes, which makes it worse: bad data travels silently. Module 3 fixes this properly with constructors.

3

Methods & Behaviour

Instance methods act on their own object

An instance method is called *through an object*: sara.introduce(). Inside the method, every bare field name refers to that same object's field. This is why the method needs no parameters to know whose name to print — the object is the hidden first argument (later you will learn its name: this).

Behaviour that reads state and behaviour that changes it
Main.java
1void introduce() {
2 System.out.println("My name is " + name);
3}
4
5void birthday() {
6 age = age + 1;
7 System.out.println(name + " is now " + age);
8}
`introduce()` only reads; `birthday()` changes state. Both belong to the object because both depend on it.
Run the Student class

Add a second student with your own name and call both methods on it.

Output
Press Run to execute this example.

Never put `static` on a method that touches fields

static void introduce() belongs to the class, so there is no object and no name to print — the compiler rejects it with 'non-static variable name cannot be referenced from a static context'. Only main and true helpers are static in this course.

Common mistakes

Wrongclass Student { static void introduce() { System.out.println(name); } }
WhyA static method has no object, so it cannot see instance fields.
FixRemove `static`: instance behaviour belongs to instances.
Wrongvoid introduce() { String name; System.out.println(name); }
WhyThat declares a *local* variable which shadows the field and has no value.
FixDeclare fields in the class body and just use `name` inside methods.
Wrongclass Student { String name = "unknown"; void setName(String n) { name = n; } introduce() { } }
Why`introduce()` has no return type, so it is not a valid method declaration.
FixEvery method needs a return type: `void introduce() { ... }`.

Lesson playground

Free practice

A Book class with three fields and one method. Add a `discountedPrice()` method that returns a double and call it.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

Two counters, one class. What is printed?

Main.java
1class Counter {
2 int value;
3
4 void bump() {
5 value = value + 1;
6 }
7}
8
9public class Main {
10 public static void main(String[] args) {
11 Counter c = new Counter();
12 c.bump();
13 c.bump();
14 System.out.println(c.value);
15
16 Counter d = new Counter();
17 System.out.println(d.value);
18 }
19}

Summary

  • `class Name { fields; methods; }` declares a new type.
  • Fields are declared in the class body, live as long as the object, and start at type defaults.
  • Instance methods are called through an object and see that object's fields with no prefix.
  • `static` on an instance method is a compile error waiting to happen.