Java Tutorial
Java Class Methods
Instance methods belong to a class and run on an object. Call them with the dot operator — they can read and update that object's fields.
Methods on an object
A method declared without static is an instance method. You need an object to call it:player.addPoints(5). Inside the method, fields like score refer to that object's data.
Example
class Player {
String name;
int score;
void addPoints(int pts) {
score += pts;
}
}
public class Main {
public static void main(String[] args) {
Player p = new Player();
p.name = "Rin";
p.score = 0;
p.addPoints(10);
System.out.println(p.name + " " + p.score);
}
}this means the current object
Inside an instance method, this is the object the method was called on. Use it when a parameter name shadows a field, or when you want to be explicit.
Example
class Player {
String name;
int score;
void setName(String name) {
this.name = name; // field = parameter
}
}
public class Main {
public static void main(String[] args) {
Player p = new Player();
p.setName("Kai");
System.out.println(p.name);
}
}Methods that return values
Instance methods can return results just like static helpers. The difference is they can base the answer on the object's fields.
Example
class Rectangle {
int width;
int height;
int area() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.width = 4;
r.height = 3;
System.out.println(r.area()); // 12
}
}You cannot call an instance method from main without an object:Player.addPoints(5) is wrong unless the method is static. Create the object first, then use the dot.
Try It Yourself
Exercise: Add a void reset() method to Player that setsscore to 0. Create a player, add points, reset, and print the score.
Show solution
class Player {
int score;
void addPoints(int pts) {
score += pts;
}
void reset() {
score = 0;
}
}
public class Main {
public static void main(String[] args) {
Player p = new Player();
p.addPoints(15);
p.reset();
System.out.println(p.score); // 0
}
}reset runs on that player, so only that object's score is cleared.
Key Takeaways
- Instance methods are called on an object with the dot operator.
- Inside the method, fields belong to the current object.
thisrefers to that object — useful when names collide.- Non-static methods need an instance;
maincannot call them as bare class methods.
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
Method on a solution
Instance methods use the object’s fields. moles() multiplies this concentration by this volume.
n = cV
Example
class Solution {
double conc, vol;
double moles() { return conc * vol; }
}
public class Main {
public static void main(String[] args) {
Solution s = new Solution();
s.conc = 0.2;
s.vol = 0.5;
System.out.println(s.moles());
}
}