Sunday, 8 January 2012

variables in java

                java variables beginers
Based  on the purpose and position of  Declaration all the variables are divided into three types
java variables beginers,java variables in java,java variables,java variables for beginers
  • Instance Variables
  • static variables
  • local variables
Instance Variables:
  • If the value of the variable varying from object to object such type of variables are called instance variables.what is variable in java?
  • For the every object separate copy  of instance variables will be created .
  • instance variables will be created at the time of creation of object and destroy at time of object destruction.hence the scope of instance variables is exactly same as scope of object.
  • instance variables will be stored in heap memory. as part of object.
  • instance variables should be declare with in the class directly but outside of any method/block/constructor. not allowed
  • we can't access instance variables directly from static area.we should access by using object reference only.
  • but from instance area we can access instance variables directly. 


Example:
class Test{
int x=10;
public void m1(){
System.out.println(x);
}


public static void main(String ar[]){
System.out.println(x);//compile-time error
Test t=new Test();
System.out.println(t.x);


}


}


D:\javaprog>javac Test.java
Test.java:5: non-static variable x cannot be referenced from a static context
System.out.println(x);
                   ^
1 error


for the instance variables it is not required to perform initialization explicitly.Jvm will always provide default values.java variables beginers


class Test{
int x;
public static void main(String ar[]){
Test t=new Test();
System.out.println("the default value:"+t.x);

}
}

  
D:\javaprog>javac Test.java

D:\javaprog>java Test
the default value:0


static varibles:


If the value of the variable  not varying from object to object such type of variables are never recommended to declare instance variables.We have to declare those variables at class level with static modifier,java variables beginers
In  the case of instance variables every object separate copy   will be created .where in the case of static varibles for entire class a single copy will be created and shared by all objects.what is a varible in java?

static  variables should be declare with in the class directly but outside of any method/block/constructor.  with static modifier not allowed.


static variables will be created at the time of class loading  and destroy at time of class unloading.hence the scope of static variables is exactly same as scope of .class file.what is a variable in java?

static variables will be stored in method area.

We can access static variables from anywhere like from instance area or static area 
 
class Test{
static int x=10;
public void m1(){
System.out.println(x);//instance area
}

public static void main(String ar[]){
System.out.println(x);//static area
}


}
static variable we can access either by using object reference or class name.But usage of class name is recommended.with in the same class we can access directly.java variables beginers


class Test{
static int x=10;
public void m1(){
System.out.println(x);
}


public static void main(String ar[]){
System.out.println(x);//directly we can access
Test t=new Test();
System.out.println(t.x);//by using object reference
System.out.println(Test.x);//by using class name


}


}


for the static variables it is not required to perform initialization explicitly.Jvm will always provide default values.this is about what is variable in java
class Test{
static int x;
public static void main(String ar[]){
System.out.println("the default value:"+x);

}
}

  
D:\javaprog>javac Test.java

D:\javaprog>java Test
the default value:0

























No comments:

Post a Comment