• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local variables

 
Nikhil Bansal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Is it mandatory to initialize local variables.

Regards

Nikhil
 
Santhosh Sivan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes very much. Instance variables will be initialised automatically with the default values, but local variable have to be explicitly initialised before using them.

Note: If u just declare a variable and dont use it anywhere in ur code, then initialising that variable is not required.
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the difference between instance variables and local variables? I tried some books but i am not clear with as the explanation was very minimal to understand
 
Henry Wong
author
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajeshwar Rao:
what's the difference between instance variables and local variables? I tried some books but i am not clear with as the explanation was very minimal to understand


Take a look at the Sun Tutorial about variables -- especially the section about "scope".

Henry
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in addition to Santhosh's post, you must be careful about local arrays for primitive types.

Regardless where array is declared (as local, instance or class variables) their elements will ALWAYS be initialised with default values.

byte, short, int and long arrays have its elements will ALWAYS have its elements initialised with default value 0.

double and float arrays with 0.0

boolean arrays with false

and finally char arrays with '\u0000'

Hope that helps
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agree with Edisandro Bessa in primitive arrays

as l understand, arrays is only one Object and array's elements are its instance variables
when you initialize one array (eg: new int[]), the array elements are initialized by default values
 
Nikhil Bansal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajeswar,

Consider the foll. class

class VariableTest
{
static int i; // class variable (belongs to class and not to any instance)
int y; // instance variable

public void sum(int z1,int z2) // local variable
{
// do some operation
}
}


Hope it helps.

Regards

Nikhil
 
kuturam chakraborty
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rajeswr,

instance variables are actually variables declared inside aclass but outside any method,while local variables are defined inside methods and do not exist once the method completes.

for example


class Myclass{

int variable1;

void myMethod(){

int i=0;

}
}

here variable1 is instance variable and i is a local variable.
you need to initialize alocal variable as they are not given any default value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic