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() {
        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;
    }
}
Learn more about classes in Java at https://www.javaguides.net/2018/11/what-is-class-in-java-with-programming-examples.html.

Related Java OOPS Examples


Comments