In this tutorial, we are going to see what is a Java class, the rules to create a Java class.
Java Class:
Class is one of the major building blocks of OOPs principles. Since Java is an Object-Oriented Programming Language, anything that we talk about the Java is going to be an object and behind the scene, it would be a class because a class is a blueprint of an object.
Ultimately, when we say about a class that simply contains the variables and the methods, that’s it. But the differentiation comes with the languages that implementing the classes. For example, every language has its own syntaxes and semantics, these syntaxes may deffer to defining a class, but the basic rules of a class will never change in any language. Because the actual class comes from the Object-Oriented Paradigm, not from the language.
So as part of this tutorial, we are going to create a Class using Java syntax.
Let’s see a simple Java Class syntax.
Java Class Syntax :
class <ClassName>{
variables variable;
methods methods;
}
Here, class
is a keyword that is used to create a class or a user-defined datatype. <ClassName> is an identifier that represents the actual name of the class.
A typical Java Class contains two parts namely variable declaration and method definitions.
The variable declaration represents what type of data members which we use as a part of the class.
Method definition represents the type of methods, which are used to perform operations inside the class.
Access Levels Of a Class:
We can use 2 types of access modifiers in front of a Java class. Those are public and default.
- If we declare a class as public, the class can be accessed across all the packages in the java application.
- If we declare a class as default, the class can be accessed only within that package.
Java Class Example :
public class Student {
private int studentId;
private String studentName;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studenttId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
In the above example, we have created a Student class with public access modifier. This class can be accessed across the application.
Members of a Java Class :
A typical Java class contains data members (variables) and member functions (methods).
Data members: Data members means variables/properties. If we take the above Student class, the data members are studentId
and studentName
.
The possible access modifiers of the data members are public
, private
, protected
and default
. You can get more information on Java access modifiers here.
The default access modifier of a data member in a class is the default
.
Member Functions: Member functions means methods. If we take the above student class, the member functions getStudentId(),
setStudentId()
and setStudentName()
, getStudentName()
The possible access modifiers of the member functions are public, private, protected and default. You can get more information on Java access modifiers here.
Happy Learning 🙂