"A class is a way of binding the data and associated methods in a single unit".
Any JAVA program if we want to develop then that should be developed with respective class only i.e., without class there is no JAVA program.
In object oriented programming?s, generally we write two types of methods. They are member methods and non-member methods.
A member method is one which is comes under the scope of the class. In JAVA we use only member methods.
Non-member methods are those which are not comes under the scope of the class. JAVA does not allow non-member methods at all.
Syntax for defining a CLASS:
Class <clsname>
{
Variable declaration;
Methods definition;
};
Here, class is a keyword which is used for developing or creating user defined data types Clsname represents a JAVA valid variable name and it is treated as name of the class. Class names are used for creating objects.
Class contains two parts namely variable declaration and method definitions. Variable declaration represents what type of data members which we use as a part of the class. Method definition represents the type of methods which we used as the path of the class to perform an operation.
By making use of the variables, which are declared inside the class? Every operation in JAVA must be defined with in the class only i.e., outside definition is not possible
Example: Define a class called a student..?
Class student { Int stno; String stname; Float marks; String cname; Int getnohoursstudy () { ............ ............ } String getgrade () { ............ ............ } } [;]
Whenever we define a class there is no memory space for data members of the class. Memory space will be created for the data members of the class when we create object.
Note:
In order to store the data for the data members of the class, we must create an object.
Note:
JAVA always follows dynamic memory allocation but not static memory allocation.
In order to create a memory space in JAVA we must use an operator called new. This new operator is known as dynamic memory allocation operator.
<Clsname> objname = new <clsname ()>
Clsname represents name of the class. Objname represents JAVA valid variable name treated as object. New is called dynamic memory allocation operator.
Clsname () represents constructor. The new operator will perform two standard actions. They are:
<Clsname> objname; //object declaration// Objname = new <clsname ()>; //object referencing//
When an object is declared where value is null. Since, there is no memory space for data members of the class. When the object is referenced the value of the object is not null. Since, memory space is created for the data members of the class