Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Local variables vs Instance variables

Thu Jan 29, 2009 4:05 pm

Local variables vs Instance variables
Here i compare between two terms in Java Local variables and instance variables .

Local variables : Are the variables declared inside the function. Their scope is limited to the function .They can be only accessed inside the function. They are deleted at the end of method.Local variables are called stack variables because they are saved in the stack.
java code
public class ProjectIT {

public static void main(String[] args) {
int x=4;
int y=4;
float sum=x+y;
System.out.print(sum+"\n");
ProjectIT projectIT= new ProjectIT();
projectIT.getNumberOfProjects();
}

public int getNumberOfProjects()
{
int numOfProjects=4;
return numOfProjects;
}
}

Instance variables: All the variables created in the class and not inside a function .Their scope is the scope of the class instance and they are saved in the heap .
java code
public class Car {
private int model;
private int gear;
private int maxSpeed;
private int tires;
private boolean startEngine;
private int fuel;
public Car() {
}

}


you may need to know that there is a variables named " static variables , these variables is declared using the static modifier ,and their scope is the scope of the class it self. static variables can be accessed from static or non-static variables .



Post a reply
  Related Posts  to : Local variables vs Instance variables
 PHP Variables     -  
 Casting Variables in php     -  
 Scope Variables in php     -  
 Apache Variables in php     -  
 Variables Are Assigned by Value     -  
 PHP session variables     -  
 Modify Session Variables     -  
 php variables in mail function     -  
 declaring variables problem     -  
 Static Methods and Variables     -  

Topic Tags

Java Basics