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.
  • new creates 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));
  }
}

FAQ: Java Data Classes

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 data class 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 data class in this Java Java lesson (Java Data Classes).

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.