Ticker

6/recent/ticker-posts

Java (Variables in Java)

VARIABLES IN JAVA


WHAT IS A VARIABLE?
Variables are the names of memory location where we temporary store our programs data.
Variables are also known as run time entities because these are used store information and data at program run time. 

In Java Programming language, variables can be classify in the three categories.
1. Instance variables
2. Static Variables
3. Local Variables






    INSTANCE VARIABLES IN JAVA
    Instance variables are those variables which are declare inside a class but outside of any method, constructor or block.

    EXAMPLE
    class Student
    {
    String name;
    int age;
    }

    In the above example name and age are the examples of instance variable in the Student class.



    STATIC VARIABLES IN JAVA
    Static variables are class variables and these are declared with static keyword. 
    Static variables are initialized only once and static variables are also used in declaring constant along with final keyword.

    EXAMPLE
    class Student 
    String Name;
    int Roll_no; 
    static int I_code=1101; 
    }

    In the above code I_code is a static variable and each object of Student class will share I_code variable's property.


    FEATURES OF STATIC VARIABLE
    1. Static variables are also known as class variable.

    2. Static means to remain as constant.

    3. In Java, it means that it will be constant for all the instances created for that class.

    4. Static variable need not be called from object.

    5. Static variables are called by classname.static variable name.

    Note: A static variable cannot be defined inside a method therefore it can never be a local variable.



    LOCAL VARIABLES IN JAVA

    Local variables are declared within the methods, constructors or blocks. 

    Local variables life line completely depend on its parent function/method. Local variables are initialized when its parent function/method, constructor or block start and it will be destroyed once its end. 

    Note: Any access modifiers cannot be use with local variables.


    EXAMPLE
    float Discount(int price)
    {
    float dis;
    dis=price*(20/100);
    return dis;
    }

    In the above example of "dis" is an example of local variable because it declared withing the "Discount" function.














    Post a Comment

    0 Comments