A class is used to specify the properties and behavior of an object. Using variables, we can implement the properties of an object, and by using methods, we can implement the behavior of an object.
Note- A class is like a blueprint using which we can create objects.
class class_name { attributes; methods; }
The object is an instance of a class. An object is instantiated with the new operator. Whenever an instantiation is performed, a heap memory address value to that object is returned and assigned to the reference variable.
Using object reference variable, you can access object's attributes with the help of dot (.) operator.
class Employee { int id; String name; public void setId(int i) { id=i; } public int getId() { return id; } public void setName(String n) { name=n; } public int getName() { return name; } public static void main(String args[]) { Employee e = new Employee(); e.setId(1); e.setName("Keshav"); System.out.println("Employee ID = "+e.getId()); System.out.println("Employee Name = "+e.getName()); } }
Output
Employee Id = 1 Employee Name = Keshav