This post will provide a profound understanding of the below-listed concepts:
- What is a Java Class?
- Why Java Class?
- How to Create a Java Class
- How to Access Class Members in Java
- Practical Implementation of a Java Class
Before heading towards the java classes first we have to understand what is the need for a java class or why someone should use a java class. So, let’s begin!
What is a Java Class
It is a description of an object’s properties and actions. Let’s assume we have to construct a building and to do so, we require a plan(blueprint). Here, the blueprint or plan represents a class while the building is an object. So, all in all, we can say that the class defines the state and behavior of an object.
Why Java Class?
Java provides primitive data types such as int, float, double, etc. Using these data types we can create variables. The primitive data types are useful when we have to store a single value in a variable such as a person’s age, name, etc.
But what if we have to store a set of information e.g. we need to store the information about an employee such as his name, age, id, department, salary, etc. We can’t store all this information in one variable.
We need multiple variables of various data types to store the employee data. But in such a case, it wouldn’t be possible to maintain the relationship of these variables (i.e.we need to group all the variables to store the data of a single student which is not possible using primitive data types).
Therefore, to deal with such situations, OOP offers the concept of classes. The class allows us to group all these variables in a single template.
How to Create a Java Class
The below code snippet will provide you with all the necessary details to understand how to create a java class:
//code
}
A Java class can have class attributes/variables, constructors, blocks, methods, and nested classes.
How to Access Class Members in Java
To access any member of the java class, we have to create and utilize the object of that class. The snippet given below will assist you in this regard:
In this way, we can create the object of some specific class using a “new” keyword. Now, consider the below snippet to understand how to access any class member in java:
object.methodName();
Using dot “.” syntax we can access any class member.
Practical Implementation of a Java Class
Let’s consider the example given below to understand the working of a Java Class.
Example
In the below-given snippet, we will create a class “EmployeeExample” that contains three class attributes: empName, empAge, and empId. Moreover, we will create a method to show the employee details “displayData()”:
String empName = "Joe";
intempId = 13;
intempAge = 27;
voiddisplayDetails(){
System.out.println("Employee Name: " + empName);
System.out.println("Employee Age: " + empAge);
System.out.println("Employee Id: " + empId);
}
publicstaticvoidmain(String[] args) {
EmployeeExample emp = newEmployeeExample();
System.out.println("Employee Name: " + emp.empName);
emp.displayDetails();
}
}
In the main method, first, we created the object of the class, and afterward, we accessed the class attributes and class method using that object:
The above snippet verified the working of the Java class.
Conclusion
A java class is a blueprint that provides the description of an object’s properties and behavior. In java “class” keyword is used to create a class. A Java class can have class attributes/variables, constructors, blocks, methods, and nested classes. The class members can be accessed using the object of that class (i.e. classObject.classMember;). This post explained various aspects of java class such as how to create a java class, how to access the class members, etc.