***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***

posts

    Variables in Java

    A variable is a name which is associated with a value that can be changed.

    For example

    int i=10; 

    here variable name is i which is associated with value 10, int is a data type that represents that this variable can hold integer values.

    To declare a variable follow this syntax:

    data_type variable_name = value;

    here value is optional because in java, you can declare the variable first and then later assign the value to it.

    Variables naming convention in java

    1) Variables naming cannot contain white spaces, for example: int num ber = 100; is invalid because the variable name has space in it.
    2) Variable name can begin with special characters such as $ and _
    3) As per the java coding standards the variable name should begin with a lower case letter, for example int number; For lengthy variables names that has more than one words do it like this: int smallNumber; int bigNumber; (start the second word with capital letter).
    4) Variable names are case sensitive in Java.

    Types of Variables in Java

    There are three types of variables in Java.

    1) Local variable 2) Static (or class) variable 3) Instance variable

    Static (or class) Variable

    Static variables are also known as class variable because they are associated with the class and common for all the instances of class.

    Example: If three objects are created of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.

    public static String myClassVar = "class or static variable";

    The static variables can be accessed without using the objects of the class.

    System.out.println(myClassVar);

    Instance variable

    Each instance(objects) of class has its own copy of instance variable. Unlike static variable, instance variables have their own separate copy of instance variable. 

    Following are the notable differences between Class (static) and instance variables.

    Instance variables

    Static (class) variables

    Instance variables are declared in a class, but outside a method, constructor or any block.

    Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

    Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

    Static variables are created when the program starts and destroyed when the program stops.

    Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference.VariableName.

    Static variables can be accessed by calling with the class name ClassName.VariableName.

    Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.

    There would only be one copy of each class variable per class, regardless of how many objects are created from it.

    Local Variable

    These variables are declared inside method of the class. Their scope is limited to the method which means that values cannot be changed and they cannot be accessed outside of the method.


    No comments:

    Post a Comment