Skip to content

Java Classes

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

Syntax

java
class <class_name> {
    // field, constructor, and
    // method declarations
}
class <class_name> {
    // field, constructor, and
    // method declarations
}

Example

java
class Student {
    int id;
    String name;
}
class Student {
    int id;
    String name;
}