Java OOP · Module 2
Writing your first class
Fields, methods, and why they are not static
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
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
class is the keyword, Student is the new type name (PascalCase). Everything between the braces is a member of the class.static, because it needs a specific student to introduce. void because it prints instead of returning.name with no prefix means *this object's* name. Java silently reads the field of whichever object the method was called on.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
Fields
Fields hold the object's state
Field vs local variable — a distinction beginners miss
| Field (instance variable) | Local variable | |
|---|---|---|
| Declared | inside the class body | inside a method |
| Lives | as long as the object lives | until the method returns |
| Default value | 0 / 0.0 / false / null | none — you must assign before use |
| Visible to | every method of the object | only its own method |
| Purpose | remember state | temporary 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.
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).
Add a second student with your own name and call both methods on it.
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.
Prove it — coding challenges
Common mistakes
class Student { static void introduce() { System.out.println(name); } }void introduce() { String name; System.out.println(name); }class Student { String name = "unknown"; void setName(String n) { name = n; } introduce() { } }Lesson playground
A Book class with three fields and one method. Add a `discountedPrice()` method that returns a double and call it.
Check understanding
Quiz
Question 1 of 4
Two counters, one class. What is printed?
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.