Wednesday, May 29, 2013

Variales and Methods In JAVA



Variables in Java:

·         Whenever we develop any project in Java, it must be developed with respect to class only. Class can contain collection of data members and methods.
·         Data members of the class are known as variables.
·         In programming the data will be represented in terms of variables.
·         It is an identifier in java whose values may be changing during the execution of program.
·         Rules for writing variables:
o   First letter of the variable name must be an alphabet.
o   The length of the variable should not exceed more than 32 letters.
o   No special symbols are allowed except underscore.
o   No reserved keywords are allowed to be used as variable names.
·         Declaration of variables:
o   Declaration is a process of providing an identity to a variable.
o   Before using a variable in our java application, it must be declared.
o   Syntax for declaring a variable;
§  data-type varName1, varName2, varName3,……………varNameN;
Here, data-type represents either fundamental data type or user-defined data types.
varName1, varname2, varName3,……………,varNameN represent java valid variable names which are used for storing the data.
                        For example:
                                    int a, b, c;
                                    float f1, f2, f3;
                                    char ch;
                                    boolean flag;
·         Declaration cum initialization of the variable;
o   It is a process of placing our own value without placing the default value whenever the memory space is created for the variable.
o   Syntax for declaring and initializing the variable:
§  data-type varName=value;
for example:
            int a=10;
            float f1 = 3.14f;
            char ch = ‘x’ ;
            boolean flag=false;
·         In java, we have 2 types of variables. They are:
1)      Instance / Non-Static Variables
2)      Static Variables


Instance variables v/s. Class variables(Static Variables)


Sr#
Instance Variables
Static Variables

1.
 Instance variable is one whose space is allocated / created each and every time whenever the object is created.
Static variable is one whose space will be created only once when the class is loaded in main memory with the help of Class Loader Sub System (a JVM Tool) irrespective to the number of objects created.

2.
Programmatically, instance variable declaration should not be preceded with the static keyword.
Programmatically, static variable declaration must be preceded with “static” keyword.
3.
Syntax:
data-type variable-name;
Syntax:
static data-type variable-name;
4.
Instance Variables must be accessed with respect to object name.
Static variables must be accessed with respect to class name.
5.
Instance Variables values are not sharable.
Static variable values are sharable.
6.
Also known as object level data members, since they are being accessed with respect to object.
Also Known as class level data members, since they are being accessed with respect to class.
7.
Instance Variables are objects dependent but class independent.
Static variable are class dependent but object independent.
For example:
class Student
{
            int rollno;                                                           
            string st_ name;
            string uni_name;
}
Student s1 = new Student();
s1.rollno = 101;
s1.st_name = “ABE”;
s1.uni_name = “RGPV”;

In memory
roll_no = 101 (4byte)
name  = ABE (16 byte)
uni_name = RGPV (8 byte)
Total=========28 bytes

If we have 100 such students of the same university name than 800 bytes will be used for the same value, since RGPV is same for all 100 students. 
  
So we are using “static” before uni_name so that value of uni_name is shared by all the 100 students.
static String uni_name = “RGPV”; (now uni_name is shared by all and take only 8byte in memory for all 100 students)

If we want to fix the value for all the 100 students (for all records) we use “static” keyword.

By using static variable we can allocate memory only one time.
We can call it by class name as
Student.uni_name = “RGPV”;

Methods in Java:

  • Operation can be performed in java in terms of methods.
  • They are the collection of statements which we can call with the single statement.
  • Based on the operation which we perform, the methods can be divided into 2 types. They are;
1)      Instance methods.
2)      Static methods

Sr#
Instance method
Static methods
1.
Instance methods are those which will be called each and every time to perform the repetitive operation with multiple values.
Static Methods are those which are called only one time to perform one time operation such as Database connection, opening a file , initialization of the data member etc.
2.
Instance method declaration should not be preceded with keyword “static”.
Static Method declaration should be preceded with keyword “static”.
3.
Syntax:
return-type method-name(parameters, if any)
{
}
Syntax:
static return-type method-name(parameters, if any)
{
}
4.
Instance methods will be called with respect to object hence called object level method.
Static methods will be called with respect to class-name hence called class level method.
5.
The result of Instance method is not sharable.
The result of static is sharable.

No comments:

Post a Comment