What is variables?

  • A variable is a name that is associated with a value that can be changed
  • When we want to store any information, we store it in the address of the computer. Instead of remembering the complex address where we have stored our information, we name that address.
  • The naming of an address is known as a variable. Variable is the name of a memory location.

How to declare a Variable?

We declare a variable using the below Syntax

Syntax {Variable}

//This is Syntax      
datatype VariableName=value;
//This is Example 
int i=10;

Variable address finding Example

package javaBasics;

public class VarEx
{

    public static void main(String[] args)
    {

        VarEx v=new VarEx();
        System.out.println("The memory address of " + v);
    }
}
//output
//The memory address of [email protected]


Variable Naming Conventions/Rules

  1. All identifiers/Variables should begin with a letter (A to Z or a to z), currency character ($), or an underscore (_).
  2. Variables naming shouldn’t contain white spaces, for example, int num ber = 100; is invalid because the variable name has space in it.
  3. A variable name can begin with special characters such as $ and _
  4. 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 have more than one word do it like this: int smallNumber; int bigNumber; (start the second word with a capital letter).
  5. Variable names are case sensitive in Java.

//Examples of legal identifiers: age, $salary, _value, __1_value.
//Examples of illegal identifiers: 123abc, -salary


Types Of Variables

1 Instance Variable/ Global Variable/ Non-Static Variable

  • They are referred to as an object variable. 
  • variables that are declared inside a class but outside any method, constructor, or block.
  • An instance variable is also the variable of an object commonly known as field or property. 
  • Object creation is mandatory to access the instance variable

Object Creation syntax and Calling the instance variable

//syntax
//classname referenceVariable=new className();
instanceVariable iv =new instanceVariable();
//calling
//System.out.println(referenceVariable.varName)
System.out.println(iv.number);

Instance Variable Example

public class instanceVariable
{
//  The instance variable is shown below
    String name="https://codershandbook.in";
    int number=7;
    public static void main(String[] args)
    {
        instanceVariable iv =new instanceVariable();
        System.out.println(iv.name);
        System.out.println(iv.number);
    }
}
//Outputs
//https://codershandbook.in
//7


2 Static Variable

  • Static are class variables declared with the static keyword. Static variables are initialized only once.
  • Generally, static means to remain constant. and it doesn’t change
  • Note– A static variable can never be defined inside a method i.e it can never be a local variable.

Static Variable Declaration Syntax

static varname;
//this Static should be use only outside the methods

Calling static variable should follow this Syntax

static varname;
//calling static 
// new className();
//System.out.println(classname.varname);

static Variable Example

public class staticVariable
{
    static String a="This is static Variable";
    static int b=7;
    public static void main(String[] args)
    {
//        Object Creation using new keyword

//        new className();
        new staticVariable();

//        Calling static Variable
        System.out.println(staticVariable.a);
        System.out.println(staticVariable.b);

    }

}
//Outputs
//This is static Variable
//7


3 Local Variable

  • Local variables are declared in method, constructor, or block. 
  • Their scope is limited to the method, constructor, or block. which means that You can’t change their values and access them outside of the method, constructor, or block..

public class localVariable
{
    public static void main(String[] args)
    {
        String s="This is local variable";
        System.out.println(s);

    }
}
//Outputs
//This is local variable


Variable Scope in Java

  • The scope of a variable decides its accessibility throughout the program. As we have seen variables are different types so they have their own scope.

Representation of scope of visibility Fig 6.1

Local variable: Scope of local variable is limited to the block in which it is declared. For example, a variables declared inside a function will be accessible only within this function.

Instance variable: scope of the instance variable depends on the access-modifiers (public, private, default)

  • private then it is accessible within class only.
  • public then it is accessible for all and throughout the application.
  • default the it is accessible with in the same package.