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.
  • this refers to that object — useful when names collide.
  • Non-static methods need an instance; main cannot 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());
  }
}

FAQ: Java Class Methods

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java class methods examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java class methods in this Java Java lesson (Java Class Methods).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.