Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes initialisation and reinitialisation doubt..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "initialisation and reinitialisation doubt....." Watch "initialisation and reinitialisation doubt....." New topic
Author

initialisation and reinitialisation doubt.....

hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
hi all, i have this doubt regarding the initialistion and re-initialisation.

code:-

public class Test1{
public static void main(String args[]){

int x= 100;
System.out.println("int x: "+x);
int x = 200;
System.out.println("int x: "+x);
}
}


public class Test2{
public static void main(String args[]){

int x;

for(x=0;x<=3;x++)
{
int y = -1;
System.out.println("int y: "+y);
}
}
}


if i compile the first program i'm getting a complie time erorr saying that variable already defined whereas if i compile the second program i'am successful.

My doubt is if a variable can be initialised only once then how in the second program we are able to initialise it in a for loop again and again whereas not in first program. Thank u in anticipation.....


.....For Every Problem There Is a Solution.....
Regards, Hem
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
the problem is not due to variable initialization, but variable declaration : in your 1st sample, you are trying to declare variable x 2 times.

If you replace int x = 200 by x = 200 it will work fine.

And if you replace the for loop by for(int x=0;... the compiler will produce an error saying w is already defined.

Note that the for loop does not initialize x value more than once, it simply changes x value.


Regards,<br />Seb<br /> <br />SCJP 1.4
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Originally posted by mohan anand:

public class Test1{
public static void main(String args[]){

int x= 100;
System.out.println("int x: "+x);
int x = 200;
System.out.println("int x: "+x);
}
}


You are declaring x twice.
Try


Joanne
hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
my doubt is that in for loop in second class how i can reinitialise 'y' again and again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

To "declare" a variable means to state its type and name, like

int x;

To "initialize" a variable means to assign a value to it when it is declared.

int x = 3;

The term "reinitialize" has no specific formal meaning in Java; we won't use that term at all here.

In any case: it is not true that "a variable can only be declared once." It is true that a variable cannot be declared twice in one block, so

int x;
int y;
int x; // Illegal

is illegal. It's also not legal to declare a local variable in an inner block that has the same name as a local variable already visible in an outer block:



But it is perfectly legal to declare a variable in a loop, so long as that name isn't already being used for a local variable outside the loop. For the purposes of compiling your code, a declaration in a loop counts as just one declaration; the fact that the loop executes multiple times doesn't matter.


[Jess in Action][AskingGoodQuestions]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10


... if i compile the second program i'am successful. my doubt is that in for loop in second class how i can reinitialise 'y' again and again.

Because you are only declaring it once each time through the loop and at the end of each pass through the loop, it goes out of scope.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
My heart-felt thanks to all the big guns who answere my question.Everybody gets a bear hug from me.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initialisation and reinitialisation doubt.....
 
Similar Threads
Doubt on static
doubt in ++ operator
Enthuware question doubt
doubt in one question
doubt in one question