Static and Dynamic Memory Allocation in JAVA


In JAVA the class contains two types of things within it which are
                                       
                       1. Data Members
                       2. Member Functions or Methods

Since Data Members represent the properties of the objects and the Methods or functions within a class represent the behavior of the object of class type.
    object is the entity which can have the Data Members and Methods of the class .

lets consider we have a class which is as follow:
                 
      class  Student{
                                String name;
                                     int roll_no;
                                     int percentage;
                         static char batch;
                              void getStudentDetails( )
                              {
                                // body;
                               }                            
                              void showStudentDetails( )
                              {
                               //  body;
                               }
                       static avgResultOfStudents( )
                            {
                             // body;
                               }

                             }
                     
In any Class there exist two types of Data members and Methods which can be
                    1. Static
                    2. Non-static ( instance )

The non-static data members or methods can also be called as instances of the class. The data members or methods which doesn't contain any static keyword in there declaration then they are called as instance member of the class. and the data members and the methods which are having static keyword in there declaration then these are termed as static members of the class.

Static Members of the Class and static memory allocation :-  

The portion of a class which is getting a memory at the loading time of the class that can get the memory only once in the life cycle of a class, are known as static member of the class and this memory allocation technique is knowns as static memory allocation.

Instance Members of a Class and Dynamic memory allocation:-  

The portion of a class which is getting memory at the run-time of the class or execution time of the class, and that can get memory again and again in the life cycle of the class, then these members are called Instance members of the class and this memory allocation techniques is called Dynamic Memory Allocation.


In the above given code or example of Student class static members are 

                                 static char batch;
                                 static avgResultOfStudents( )
                                   {
                                          // body;
                                    }

so, these two things are getting memory at compile - time. But in JAVA every function will get memory at run-time only. Then, how can we say the method avgResultOfStudents( ) is a static one?
yes, it is the static one  because the at compile time the compiler will set some set of instructions for the memory allocation of  avgResultOfStudents( ). And these instructions will get called whenever it is being used, so avgResultOfStudents( ) have static allocation and it is static memory allocation.

while for the all the other members of the class will have the dynamic memory allocation.

                               String name;
                                     int roll_no;
                                     int percentage;
                         
                              void getStudentDetails( )
                              {
                                // body;
                               }                            
                              void showStudentDetails( )
                              {
                               //  body;
                               }
                  
All the above given data will get memory at the run-time so they are implements dynamic memory allocation.

Comments

Popular posts from this blog

Screen Saver Development In C language

Installing MySQL(5.7) on MacOS (Monterey)