This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anything wrong in this code ????

 
sekhar subbu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Person
{
int i=9 ;
{

int i=10 ;

}

public static void main(String args[])
{
int u=90;
{

int u=190;

}

}

}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking for an explanation or posing a challenge?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hit me !
 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not compile. Try changing

int u=190;

to

int j=190;


and it would work just fine. Previous declaration of int i would not be seen as a duplicate as they are in separate blocks of code, thus their scope is limited to their blocks. The repitition of
u
in main is not allowed however.
 
sekhar subbu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Murad Iqbal:
It will not compile. Try changing

in main is not allowed however.


Hi
thanks a lot for the explanation....actually i saw it in a java test question paper and i was wondering abt the answer?

anyways now i got the answer .

But i wanted to know why does in main ....we cant have that redeclaration like in class ?
 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not about main. Its about method. You can not have that redeclared in a method. In a class you can.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic