CLASSES , OBJECTS , FIRST PROGRAM


                                    CLASS

* Perhaps the most important thing to understand about a class is that it defines a new data type ( that is our own data type ). Once defined , this new type is used to create object of that type. thus a class is a template for an object , and an object is an instance of a class .

* Class is a collection of fields and methods , fields are used to hold the data whereas methods are used to perform the operation on the data .

* The default access modifier for class with respect to C++ is private .

* The default access modifier for class with respect to Java is default .

*The class members can’t be initialised in C++ , whereas in Java we can initialise  .

THE GENERAL FORM OF A CLASS:


        class ClassName

  {

    type instance-variable1;

    type instance-variable2;

    //..

    type instance-variableN;


   type methodname1(parameter-list(formal arguments))

     {

        //body of the method

     }

   type methodname2(parameter list)

     {

        

         //body of the method

     }

     //….

   type methodnameN(parameter list)

     {

        

        //body of the method

     }

}

  The data or variables , defined within a class are called instance variables or instance fields . The code is contained within the method . Collectively , the methods and variables defined within a class are called members of the class . In most classes , the instance variables are acted upon and accessed by the methods defined for that class . Thus it is the method that determine how a class data can be used .

             Variables defined within a class are called instance variables because each instance because each instance of the class ( that is each object of the class ) contains its own copy of variables . Thus , the data for one object is separate and unique from the data for another object. Java classes do not need to have a main() method . You only specify one if that class is the starting point for your program . Further , applets do not require main() method at all .

                One main important point to remember is classes are just the logical representation of the data and operations , it does not hold any space in the memory . That is it does not hold the data , the  actual  memory allocation is done through object of that class. Object is the instance of a class . classes does not allow you to store the data , the actual storage is done ‘through object : in object’ . 

INSTANCE FIELDS :

  • The fields which are defined within the class without static modifier than that fields are called as instance fields .
  • For every instance fields , memory is allocated within the object .
  • The every instance field by default is initialised with their corresponding values.

DECLARING OBJECTS :

As just explained , when you create a class , you are creating a new data type . You can use this type to declare objects of that type . However , obtaining objects of a class is a two-step process . First , you must declare a variable of the class type .This variable does not define an object .  Instead , it is simply a variable that can refer to an object . Second , you must acquire an actual , physical copy of the object and assign it to that variable. You can do this by using new operator . The new operator dynamically allocates ( that is, allocates at run time ) memory for an object and returns a reference to it . This reference is, more or less , the address in memory of the object allocated by new . 

In simple words , object is nothing but a storage space in the memory, that is we require a physical representation called object . In order to access the properties of  the class we need to create object . 

     To create an object in the Java language , we require two things , that is 

                  1. Reference variable 

                 2. New and constructor 

syntax for create a reference variable 

<class TypeName> <reference variable>


example : Student s; 

// Student is the name of the class and ‘s’ is the reference variable 

**** For reference variable memory is allocated in the ‘stack‘ organisation . For every reference variable 4bytes of memory is allocated . Therefore , s reference variable occupies 4bytes of memory.

The reference variable is existed with 2 parameters 

                    1.Type

                    2.Hashcode

Type is holding the name of the class (that is , Student ) and hashcode is holding the address of a object ( we still didn’t create a object )

syntax for creating a ‘object‘ :

<reference variable>=new <class name>();

example s=new Student();

 // here ‘s’ is the reference variable ‘new’ is the operator and Student() is the constructor .

Therefore , declaring reference variable and creating object can be done in one step by combining the two steps a Student

                s=new Student();

The ‘new‘ is the dynamic memory allocation operator , which is used to allocate the memory dynamically , during execution in the ‘heap‘ organisation (that is , object is stored in heap organisation) . where as the reference variable is stored in stack organisation.

      As and when the , constructor ( that is Student()) of the class is invoked ( that is , when the controller comes to constructor ) the object of the class is instantiated . For every object the JVM will give a new unique identity called hashcode . As and when the object is instantiated whatever the fields which are existed in the class ( that is, instance variables which is declared inside the class but in the method) . for them memory is allocated within the object but not for methods .

***We can create N number of objects for a class

IMPORTANT TO REMEMBER :

Let’s once again review the distinction between a class and a object . A class creates a new data type that can be used to create objects . That is , a class creates a logical framework that defines the relationship between it’s members .  when you declare an object of a class , you are creating an instance of that class.. Thus , a class is a logical construct. An object has physical reality . that is , an object occupies space in memory . It is important to keep this distinction clearly in mind .

                                   FIRST PROGRAM

PROGRAM ON OBJECTS HASH CODE IN THE MEMORY : 

CLICK ON THE IMAGE

memory address of two objects



**** IF YOU HAVE ANY DOUBTS REGARDING THIS PAGE PLZZ COMMENT 🙂 THANK YOU

              

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: