Java Tutorial

Class & Object

Class

"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.

class digram

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:

  1. In JAVA memory space for the data members will be creating on heap memory (Dynamic memory).
  2. Memory space for methods will be creating on stack memory (that too when we call the methods).
  3. All constants of any JAVA program is available in associative memory (retrieving data from associative memory is negligible).
  4. The class definition exists only one time but whose objects can exists many number of times i.e., a class is acting as a formula form.

OBJECT

In order to store the data for the data members of the class, we must create an object.

  1. Instance (instance is a mechanism of allocating sufficient amount of memory space for data members of a class) of a class is known as an object.
  2. Class variable is known as an object.
  3. Grouped item (grouped item is a variable which allows us to store more than one value) is known as an object.
  4. Value form of a class is known as an object.
  5. Blue print of a class is known as an object.
  6. Logical runtime entity is known as an object.
  7. Real world entities are called as objects.

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.

Syntax-1 for defining an OBJECT:

<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:

  1. It allocates sufficient amount of memory space for the data members of the class.
  2. It takes an address of the class and stored in the left hand side variable of syntax-1.

Syntax-2 for defining an OBJECT:

<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