© Mikael Olsson 2022
M. OlssonJava 17 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-7371-5_10

10. Class

Mikael Olsson1  
(1)
Hammarland, Länsi-Suomi, Finland
 
A class is a template used to create objects. Classes are made up of members, the main two of which are fields and methods. Fields are variables that hold the state of the object, whereas methods define what the object can do—the so-called behaviors of the object.
class MyRectangle
{
  int x, y;
  int getArea() { return x * y; }
}

Object Creation

To access a (non-static) field or method from outside the defining class, an object of the class must first be created. That’s done using the new keyword, which will create a new object in the system’s memory.
public class MyApp
{
  public static void ...

Get Java 17 Quick Syntax Reference: A Pocket Guide to the Java SE Language, APIs, and Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.