Core Java Tutorial

Main  
 

Variables

These are also called fields and Variables in java are used to store a value.Let's see the syntaxa and following with an example.

Key points about variables

  1. you can change the value of variable whenever you want to change
  2. variable name can be start with a alphabet or Dollar($) sign or underscore( _ ).
  3. variable name can't be a number or special keywords except dollar sign($) and underscore( _ ).
  4. First letter in a word should start with lower case and after that each first letter in a word should start with capital(Ex: if you want to declare name as addnumber then it should be like this addNumber).
     Syntax:
                datatype name variable name = value;
Data type and Variables

Here int is data type of integer value 50 for the variable value.There are mainly three different types of variables are in java, they are

     1.Local variable
     2.Instance variable
     3.static variable

Local Variables

Local variables declared in side a method, they don't have any modifiers attached to them and local variables will destroy once method returns a value.There is no default value for this local variable so you should intialize their value before using them.Moreover that local variables are visible to only those methods.

               public void add(){
                 int a=10; // This is Local variable
                 int b=20; // This is Local variable
                 int c = a+b; // This is Local variable
                  }

Instance Variables

These are declared outside the mothod and inside a class, these will have modifiers attached to them.instance variables will create if you create object and destroy once object is destroyed.These variables are visible to other methods also.Instance variables have default values, it is null for object refence values and false for boolean values.

                 public class VarDemo{
                    String name="Variables"; // This is instance variable
                  public void outPut(){
                     int a=10; // This is Local variable
                     int b=20; // This is Local variable
                     int c = a+b; // This is Local variable
                    System.out.println("Name is:"+name); // here i am using instance variable 
               }
          }

static Variables

static variables are declared using static keyword and these all are stored in static memory.visibility of static variables are similar to instance variables and are declared inside a class and outside a method.static variables are called by using classname.variablename.

                 public class VarDemo{
                    String name="Variables"; // This is instance variable
                      static int a =25;
                  public void outPut(){
                     int a=10; // This is Local variable
                     int b=20; // This is Local variable
                     int c = a+b; // This is Local variable
                    System.out.println("Name is:"+name); // here i am using instance variable 
                   public static void main(String[] args){
                      VarDemo vd = new VarDemo(): // this is oject
                         System.out.println(VarDemo.a);

               }
          }
        }
    Output: 25
Main