aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local variables" Watch "Local variables" New topic
Author

Local variables

Nikhil Bansal
Greenhorn

Joined: Mar 21, 2006
Posts: 21
Hi All,

Is it mandatory to initialize local variables.

Regards

Nikhil
Santhosh Sivan
Greenhorn

Joined: Feb 14, 2006
Posts: 2
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

Joined: Mar 20, 2006
Posts: 31
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
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
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

Joined: Mar 21, 2006
Posts: 21
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

Joined: Jan 23, 2006
Posts: 25
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variables