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

  • class defines a new type with fields (and later methods).
  • new creates 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());
  }
}

FAQ: Java Classes and Objects

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 classes 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 classes in this Java Java lesson (Java Classes and Objects).

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.