Total members 11895 |It is currently Thu Dec 12, 2024 12:45 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 .



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Local variables vs Instance variables
 PHP Variables     -  
 Variables Are Assigned by Value     -  
 PHP session variables     -  
 Casting Variables in php     -  
 Scope Variables in php     -  
 Apache Variables in php     -  
 Static Methods and Variables     -  
 The using of pointers between two variables (Swaping)     -  
 Print all server variables     -  
 unset reference variables     -  



Topic Tags

Java Basics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com