Java Class Example

This post shows what is class in Java with an example.

What is a class?

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.
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:

Java class example

Let's demonstrate how to create a Class in Java with an example. 
Here is a Student class:
package net.javaguides.corejava.oops;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Student {
    private String name = "Ramesh";
    private String college = "ABC";

    public Student() {

    public Student(String name, String college) {
        super(); = name; = college;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCollege() {
        return college;

    public void setCollege(String college) { = college;
Learn more about classes in Java at

Related Java OOPS Examples

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course