Defining a Class in java
In Java programming, a class is a user defined object-oriented programming (OOP) based multiple database program design development fundamental programming concept. It can be used as a readymade template for the Java developer to create multiple types of classes (instances) for multiple objects. A class in a Java program helps to define the class attributes (fields) and class behaviour (methods). Where the Java developer can develop more than one class object parameter instance as per his requirement.

Structure of a class in Java programming.
- Class declaration – A basic Java starts with the reserved class keyword followed by the name of the class.
- Class fields (variables) – These preview the data or position of the class object in the current class.
- Class methods – These indicate the class behaviour or class procedure that can be performed on the class object in the current class program.
- Constructor – This is a special method in a class, which is used to initialize the object of the class.
General syntax of a class in Java.
// Declaring a class
public class ClassName {
// class Fields (attributes/variables)
Class dataType fieldName;
// create classs Constructor
public ClassName() {
// create class Code to initialize object
}
// create class Methods (behavior)
public returnType methodName() {
// create class Code for method
}
}
Example of a class in Java programming.
// decalre class named with employee
public class Emoloyee {
// create employee class Fields (attributes)
String employeename;
int age;
int contact;
// let create Constructor used to initialize a new object value
public Employee(String employee, int age, int contact) {
this.employeename = employeename; // use to display employee name
this.age = age; // use to display employee age
this.contact = contact; // use to display employee contat
}
// create Method with display the information of the employee
public void EmployeeInfo() {
System.out.println(“\n employee name – ” + employeename);
System.out.println(“\n employee age – ” + age);
System.out.println(“\n employee contact – ” + contact);
}
// clas Main method where the program execution starts
public static void main(String[] args) {
// let Creating an object (instance) of the employee class
Employee myEmployee = new Employee(“ajit”, 39, 9414);
// let Calling the employeeInfo method to print details of the employee class
myEmployee.EmployeeInfo();
}
}
Explanation of Java Class Example.
Java Class Declaration.
In any Java program, you can declare a class with the public class reserved keyword. Remember, at the time of class declaration in Java, the name of the class should start with a capital letter as per Java rules. For example, (employee).
Java Class Fields (Attributes).
Here in the above class program, we preview the employee name, age, and contact field attributes of the employee class in the class field from the name of the employee class.
Constructor in Java.
Java constructor initializes multiple different class objects for Employee class associated with public class Employee in (String employee name, int age, int contact).
Java Method.
In the current program declare employeeinfo() class method to print all object instance information of Employee class in console screen.
Main method in Java.
Main method is the entry point of Employee class in Java program. Where, we create an instance object of Employee class using new keyword by passing values to class constructor. After that we call employeeinfo() class method on myemployee object to print employee class information.
Java Class Key Points.
- Java Class Object – An object is an instance of a class for Employee class in Java program. Where you create an object of the class by using the new keyword in the existing program and calling the employee class constructor.employee myemployee = new employee(“ajit”, “39”, 9414);
- Access Modifiers – Any Java program class, method and field can have class access modifier objects of public, private, protected etc. nature. While the public access modifier in the class allows you to access the class object from anywhere in the class, the private class members are accessed and called only from within the class.
- Constructor – In a Java program, the constructor initializes a class object in the class program while creating it. Here, if you do not create any constructor in the class, then Java programming automatically creates a default constructor.
- Methods – Methods in a Java program define the behaviour of a class. It accepts the argument parameter in the class, and can return the value according to the method. In Java, you can invoke any calls methods using the object.
Advantages of Class in Java.
- Java Encapsulation Features – In Java program, class data (fields) and class methods are combined together in a class and processed.
- Class Reusability Features – One of the best features of Java class is that you can create multiple objects for the same class.
- Class Abstraction – Class abstraction in Java hides a class’s internal execution process, and imposes a public interface (methods) for Java class communication.