Java class is a set of related objects. The object(addition a1=new addition();a1 is the object) has some attributes, whose value consists much of the state of an object. Class is a user-defined data type in Java. The complete set of data and code of an object can be made a user-defined data type with the help of a class.
Classes have interfaces that define which part of an object of a class can be accessed from outside and how. A class body implements the operations in the interface and the instance variables that contain the state of an object of that class.
A class is a blueprint from which individual objects are created(class addition(addition a1=new addition();)). Everything in Java is defined in a class(class addition). In other words, we can say, a class just defines a collection of data for example:
class Student {
String name;
int roll-on;
String course;
}
The order of data fields(int a,b,c ; ) and methods(public void getdata() in a class are not significant. A class can contain the following variable types.
Local Variable
Instance Variable
Class Variable
Example of a class and object
class addition //addition is a class name and class is a keyword
{
int a,b,c ; // instance variable
static int si=100; //class variable
public void getdata()
{
a=20;
b=30;
}
public void setdata()
{
c=a+b;
}
public void showdata()
{
System.out.println(“Instance variable of a class=”+c);
int loc=67; //Local variable
System.out.println(“Local variable=”+loc);
System.out.println(“class variable=”+si);
}
public static void main(String arg[])
{
addition a1=new addition(); //a1 is the object of class addition
a1.getdata();
a1.setdata();
a1.showdata();
}
}
Objects in Java
Objects in Java are the basic run-time entities in an object-oriented system. They may represent(objects like) a place, a bank account, or a person. They may also represent user-defined data such as vectors, arrays, interfaces, time, and lists.
They absorb space in memory that keeps its position and is operated on by the defined operations on the object. Each object(addition a1=new addition()) contains data and source code to manipulate the data. Objects(in this example a1 is the object of class addition) can interact without having to know details of each other data or code.
Objects are behavior and state. e.g A cow has state and behavior such as color, name, types as well as behavior-eating.
Software objects also have a state and behavior. A software object’s state is stored in fields and behavior is shown via methods(getdata()).
What is an instance?
In Java language, where each object is created from a class((addition a1=new addition())), it is called an instance of that class. Creating an instance(addition a1=new addition()) of a class is sometimes referred to as instantiating the class.
A real-world example of an object would be “Doberman”. Which is an instance of a class called ‘Dog’.The data members of a class are also referred to as instant variables. These instant variables are embedded automatically within an object at the time of its creation. Hence, an Object is referred to as an instance of a class.