• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

initialisation and reinitialisation doubt.....

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my doubt is that in for loop in second class how i can reinitialise 'y' again and again.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


... 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.
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My heart-felt thanks to all the big guns who answere my question.Everybody gets a bear hug from me.....
 
Everybody! Do the Funky Monkey! Like this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic