Java is an object-oriented programming language so you can create the java class and object to divide problems into smaller sets.
Java Class
A class is a blueprint of the object. In general, you can say that class is a collection of objects which have many common characteristics. A Java class can have fields, methods, and constructors, etc.
Creation of Java class
you can easily create a class in java by using the class
keyword followed by class_name.
Syntax:
class class_name {
//fields or variables
//methods
//constructors
}
Let’s see an example of the class-
class Print{
String message="this is an example of the class";
}
In the above example, Print
is a class name andmessage
is the name of the variable.
Java Object
An object is an instance of a class and to access the members of the class, an object is needed to create. an object is a runtime entity and it has two characteristics-
- state
- behavior
Accessing the Class Members
Let’s see the example to access the members of the class using objects.
class Print{
String message="This is an instance variable"
public static void main(String[] args){
Print m=new Print();//creating an object
System.out.println(m.message);//accessing member
}
}
It will display the following results-
This is an instance variable