Class in Java

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.

A real-world example is Circle. Let’s look at an example of a class and analyze its various parts in a below diagram. This example declares the class Circle, which has the member-variables x, y, and radius of type Integer and the two member-methods, area()and fillColor().


A class is a template for creating objects

Below diagram shows a Circle class which is a template to create three objects:

Implementation with Example - Creating Student Class

Let's demonstrate how to create Class in Java with an example. Here is a Student class:
public class Student {
  private String name = "XYZ";
  private String college = "ABC";
  public Student() {
    super();
  }
  public Student(String name, String college) {
    super();
    this.name = name;
    this.college = college;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getCollege() {
    return college;
  }
  public void setCollege(String college) {
    this.college = college;
  }
}