Java Tutorial
Java Data Classes
A class can group related fields under one name — a point, a player, a cart line — so related values travel together instead of as a pile of loose variables.
Fields under one name
Before methods and privacy, think of a small class as a named bundle of fields — like a struct. You declare the fields inside the class, then create an object with new and fill them with the dot operator.
Example
class Point {
int x;
int y;
}
public class Main {
public static void main(String[] args) {
Point p = new Point();
p.x = 3;
p.y = 4;
System.out.println(p.x + " " + p.y);
}
}Point is the type. p is one object of that type. Each field lives on that object, not as a free-floating variable in main.
The dot operator
Write p.x to reach field x on object p. Change one object's fields and another object of the same class stays untouched — each instance has its own copy.
Example
class Point {
int x, y;
}
public class Main {
public static void main(String[] args) {
Point a = new Point();
a.x = 1;
a.y = 2;
Point b = new Point();
b.x = 10;
b.y = 20;
System.out.println(a.x + "," + a.y); // 1,2
System.out.println(b.x + "," + b.y); // 10,20
}
}Why group fields
Three loose variables — name, score, lives — work until you need two players. A class keeps each player's data together so you cannot mix Rin's score with Kai's lives by accident.
Example
class Player {
String name;
int score;
int lives;
}
public class Main {
public static void main(String[] args) {
Player rin = new Player();
rin.name = "Rin";
rin.score = 10;
rin.lives = 3;
System.out.println(rin.name + " score=" + rin.score + " lives=" + rin.lives);
}
}Later chapters add constructors, methods, and private fields. Here the goal is only grouping data. When a class is nothing but immutable data, a record (Advanced) can write the boilerplate for you.
Try It Yourself
Exercise: Create a Book class with String title andint pages. Build one object, set both fields, and print them on one line.
Show solution
class Book {
String title;
int pages;
}
public class Main {
public static void main(String[] args) {
Book b = new Book();
b.title = "Dune";
b.pages = 412;
System.out.println(b.title + " (" + b.pages + " pages)");
}
}The class holds the shape; new Book() makes one instance; the dots fill and read its fields.
Key Takeaways
- A class can group related fields under one type name.
newcreates an object; the dot operator reads and writes its fields.- Each object has its own copy of the fields.
- Grouping data now prepares you for methods, constructors, and encapsulation later.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
A force with components
A data class holds Fx and Fy as fields. Magnitude can wait; first you store the pair that came off the sensors.
F = √(Fx² + Fy²)
Example
class Force {
double fx, fy;
}
public class Main {
public static void main(String[] args) {
Force f = new Force();
f.fx = 3.0;
f.fy = 4.0;
System.out.println(Math.hypot(f.fx, f.fy));
}
}