• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

doubt regarding var declaration!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!!!
It would be helpful if any of you would explain as to why
i get compilation err for the following code:

for(int i=0; i<5; i++)
String tmp = "test"; // Compilation error: var redeclaration
while the code below compiles without any err :

for(int i=0; i<5; i++)
{
String tmp = "test"; // Compiles properly!!!
}

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any assignment in for loop has to be done done inside braces...
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess variable declaratons can not go anywhere. In this case, it looks like it needs the beginning of a block "{" to be a valid place for a declaration.
Can someone confirm?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friend,
See when you declare any thing in Java within the '{ }' variable will be having life only inside the block. letus take your code
class xx {
public static void main(String xy[]){
for(int i=0;i<5;i++)
String s = new String("mystring");
}
}
here variable 's' has its life inside the main method so once you created it is alive until you come out of main method.
but in your second case 's' will have life only inside your '{}' each time you go out 's' will loose its life.

I hope this will help you
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code does not compile either. It has the same problem as the original code. I do not think that scope is the issue here. It appears that the "{" is needed for the declaration to be legal.
Need more help!

Originally posted by jafarali:
Friend,
See when you declare any thing in Java within the '{ }' variable will be having life only inside the block. letus take your code
class xx {
public static void main(String xy[]){
for(int i=0;i<5;i++)
String s = new String("mystring");
}
}
here variable 's' has its life inside the main method so once you created it is alive until you come out of main method.
but in your second case 's' will have life only inside your '{}' each time you go out 's' will loose its life.

I hope this will help you


 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, shouldn't a "ranch hand" have a "proper name"
 
jafarali
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know you mean about me. Any how my name is same. JafarAli
and I am from Middle East region
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!!!
well what i think is that its a problem with scope.
when u r compiling in first case
u r declaring variable 5 times with same name which is invalid
in second case
when u enter a for block{}
u declare a string variable assign a value then after scope ends
it is destroyed .
again when u enter the for block second time
again the process is repeated i.e ..declaration and assignment
that mean everytime your for loop runs in second case
string variable is created and destroyed
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shilpa H & jafarali , welcome to javaranch.
Please read this message. PROPER NAMES ARE NOW REQUIRED

Please read the official naming policy of javaranch. User has to register himself or herself with proper first & last name. So , I request you to reregister yourself with proper name & help maintain the decorum of the forum.

YOUR FRIENDLY BARTENDER
SHAILESH

[This message has been edited by shailesh sonavadekar (edited November 26, 2000).]
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The following code runs without any error
public class scope{
public static void main(String[] args){
String tmp;
for(int i=0; i<5; i++)
tmp = "test";

}
}
This shows that the problem is with scoping..
HTH
Prasad
------------------
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!
Thanx JafarAli and Amrit the explaination was helpful!!

Rgds,
shilpa
 
His brain is the size of a cherry pit! About the size of this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!