Java Tutorial
Java Classes and Objects
The keyword class names a new type. Declaring a variable of that type and calling new creates an object — one living instance of the blueprint.
class names a type
Inside a class you list fields (and later methods). Once the class exists, its name works like int: you declare variables of that type. The difference is that objects are created with new, not with a literal number.
Example
class Player {
String name;
int score;
}
public class Main {
public static void main(String[] args) {
Player p = new Player();
p.name = "Rin";
p.score = 10;
System.out.println(p.name + " " + p.score);
}
}Two objects, two sets of fields
Each new creates a separate object. Change a.score and b.score stays the same — that is the point of instances.
Example
class Player {
String name;
int score;
}
public class Main {
public static void main(String[] args) {
Player a = new Player();
a.name = "Rin";
a.score = 10;
Player b = new Player();
b.name = "Kai";
b.score = 14;
System.out.println(a.name + " " + a.score);
System.out.println(b.name + " " + b.score);
}
}Run this in /java/try. After both objects exist, set a.score = 99. Only Rin's score changes — Kai's object is untouched.
Default field values
Unlike local variables, fields get defaults if you never assign them: 0 for numbers,false for boolean, and null for references. Still, assigning real values early makes programs easier to read.
Example
class Flag {
boolean on;
int count;
String label;
}
public class Main {
public static void main(String[] args) {
Flag f = new Flag();
System.out.println(f.on); // false
System.out.println(f.count); // 0
System.out.println(f.label); // null
}
}Try It Yourself
Exercise: Define a Dog class with String name andint age. Create two dogs with different values and print both.
Show solution
class Dog {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Dog a = new Dog();
a.name = "Mochi";
a.age = 3;
Dog b = new Dog();
b.name = "Bean";
b.age = 5;
System.out.println(a.name + " " + a.age);
System.out.println(b.name + " " + b.age);
}
}One class, two objects — each with its own field values.
Key Takeaways
classdefines a new type with fields (and later methods).newcreates an object; the variable holds a reference to it.- Each object has its own copy of the fields.
- Fields have defaults; local variables do not.
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.
Chemistry
n = cV
Amount of substance is concentration times volume in litres. A class holds both numbers and a method that multiplies them.
n = cV
Example
class Solution {
double conc, vol;
double moles() { return conc * vol; }
}
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
sol.conc = 0.100;
sol.vol = 0.25;
System.out.println(sol.moles());
}
}