JAVA FOUNDATIONS · JAVA 17
Java classes and objects: model two independent scorecards
Create objects with constructors and see how separate instances hold separate state.
Forsk Coding School learning resources · Updated 2026-10-26
What you will learn
- Distinguish a class from an object
- Initialize fields through a constructor
- Call an instance method on the correct object
Before you start: You can follow variables, methods and arithmetic.
Model a thing with state and behaviour
A class defines the fields and methods available to its objects. An object is one instance of that class. A scorecard needs a student name and two marks as state, plus a method that calculates an average. Creating two scorecards should not cause one student's marks to replace the other's. That independence is the central point of this example.
The small Scorecard class is placed in the same file as public class Main and is not public. This keeps the program runnable as one Main.java file. In a larger project you would normally organize related classes into their own files and packages. File organization can grow after the relationship between state and behaviour is clear.
What a constructor does
new Scorecard("Asha", 70, 90) constructs a Scorecard and invokes its constructor. The constructor has the class name and no return type. Its parameters are values supplied by the caller. this.name identifies the current object's field, while name identifies the parameter. The assignment copies the supplied reference or primitive value into that object's field.
average is an instance method, so asha.average() operates on the fields belonging to asha. It divides by 2.0 to preserve a fractional result. This first example exposes fields to keep object behaviour visible. The encapsulation lesson adds private fields and validation so other code cannot change an object into an invalid state.
References are not duplicate objects
Assigning Scorecard alias = asha would copy a reference to the same object; it would not create a second scorecard. A second new expression creates another instance. This distinction explains many debugging problems in which changing data through one reference appears to change data seen through another reference.
Before adding inheritance or a database, check the simple model: create two objects with different inputs and verify that each returns its own result. Then test a case with an odd total to expose integer division. Object-oriented design becomes easier to assess when each object has a small, observable responsibility.
Run the worked example
Save as Main.java. Run javac Main.java, then java Main.
class Scorecard {
String name;
int first;
int second;
Scorecard(String name, int first, int second) {
this.name = name;
this.first = first;
this.second = second;
}
double average() {
return (first + second) / 2.0;
}
}
public class Main {
public static void main(String[] args) {
Scorecard asha = new Scorecard("Asha", 70, 90);
Scorecard aman = new Scorecard("Aman", 60, 71);
System.out.println(asha.name + ": " + asha.average());
System.out.println(aman.name + ": " + aman.average());
}
}Expected result
Asha: 80.0
Aman: 65.5- The two new expressions create separate objects.
- The constructor stores different values in each instance.
- Calling average on each reference reads that instance's marks.
Common mistakes to check
- A constructor has no void or other return type.
- Declaring a reference variable alone does not construct the object.
- A static field would be shared at class level; these marks should be instance fields.
Try it yourself
Create a Rectangle class with width and height, a constructor and an area method. Construct rectangles 3 by 4 and 2 by 5, then print their areas.
Give me a hint
Store the dimensions as instance fields and return width * height.
Compare with one working solution
class Rectangle {
int width;
int height;
Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
int area() { return width * height; }
}
public class Main {
public static void main(String[] args) {
System.out.println(new Rectangle(3, 4).area());
System.out.println(new Rectangle(2, 5).area());
}
}Expected result
12
10Quick check
Choose an answer, then check the explanation. Your answer stays in this page and is not saved as a student record.
Keep learning
Explain the result in your own words, change one input and predict the new output. For additional structured assignments, explore the workbook sample before requesting the full pack.
Explore the practice workbookReference documentation
Use the official reference to check language details. The explanations, examples and exercises on this page are written for this learning resource.
