Defines a new data type which is used to create objects of that type. It is a template for an object and is a logical construct which defines the relationship between its members.
It is an instance of a class (similar to a variable declared for a data type, an object is declared for a class type). It has physical reality (ie., an object occupies space in memory).
Syntax for the class declaration:
type instance_variable l;
//body of the method -› code is contained within methods
//body of the method
} //class definition ends here
Data or variables defined within a class are instance variables. However, static variables are called class variables.
The methods and variables defined within a class are called members of the class. This way of binding. the data & the methods accessing them, together is called encapsulation.
Let us create a class named 'computer' which should contain the following as its instance variables.
RAM size, hard disk capacity, frequency, make (company name).
Also, it should have 2 methods:
i) getdata( ) to read the values tbr the instance variables
ii) display( ) to display the values of the instance variables
void getdata( )
Scanner sc = new Scanner(System.in);
RAM = sc.nextInt( );
hdisk = sc.nextInt( );
freq = sc.nextFloat( );
make = sc.nextLine( );
void display( )
System.out.println("RAM Size:" + RAM);
System_out.println("Hdisk Capacity:" + hdisk);
System.out.println("Frequency:" + freq);
System.out.printlii("Brand:" + make);
Now to work on a variable, which would have the same instance variables and methods as that of the class 'Computer', an object of this class type should be created.
As Java follows dynamic memory allocation, 'new' operator should be used, for creating objects.
Computer node = new Computer( );
'new' operator does 3 things
- Allocates memory space for the object of type 'Computer'.
- Invokes the constructor of the class (will be discussed later).
- Returns the starting Address (Reference) of the object, thus created.
In order to store this reference, a reference variable of same type 'Computer' should be created. Here, a node is just a reference variable which refers to the actual object. It is not an object, by itself.
The above statement can also be written as
Computer node; ->After this line executes, 'node' contains the value 'null',
node = new Computer( ); ->indicating that it does not yet point to an actual object.
All the methods inside the class 'Computer' — [getdata( ) & display( )] can access the instance variables RAM, hdisk, ... directly. Apart from these 2 methods, if these variables are tried for access from any other method, it can be done only with the help of an object. No direct access is allowed from outside.
To write code which can access these class members, the main( ) method should be defined, where the object for this class can be declared and used for accessing the class members.
Note: Though the object reference is similar to a pointer in C or C++, we cannot make this object reference to point to an arbitrary memory location (or) manipulate it like an integer.