What is Class in Java?

Class :-

  • A Class is a blueprint of object. That is not physical exist.
  • The class is at the core of java. It defines the shape and nature of an object.
  • A class code defines the interface to its data
  • A class is declared by use of the class keyword.
  • The data and variable defined within the class is called instance variables.
  • The method and variables defined within a class are called members of the class.

              Creating Employee object :-

                         Employee emp=new Employee();   // creating a Employee object called emp

              After this statement execution emp will be an instance of Employee Class

  • When we create a class, we create a new data type. We can use this type to declare object of that
  • First we must declare a variable of the class type. it is simply a variable that can refer to an object.
  • In second step we must acquire an actual, physical copy of the object and assign it to that variable.
  • We can using the new operator to dynamically allocates memory(at time of run time) for an object and returns a reference to it.
  • In java all class objects must be dynamically allocated.

                               Employee emp=new Employee();

                    Employee emp;                      // declare reference to object

                     emp=new Employee();             // allocate a Employee object

  • The first line declares emp as a reference to an object of type Employee class. After this line executes emp contains the null value.
  • The second line allocates an actual object and assigns a reference to it to emp, After this line executes we can use emp as if it were an Employee object. emp reference holds the memory address of an actual Employee object.
				
					public class Employee {
	String name; // type instance variable
public void test(int i) {
		// body of method
	}
	public static void main(String[] args) {
		Employee emp = new Employee();
	}
}