Tuesday, November 18, 2008

Published 11/18/2008 by with 0 comment

is already defined

If we declare a variable in a method more than once than we see ' is already defined'. Find out the variable for which the message is shown and check whether you have declared the variable more than one time in a particular block.

If we compile the following code we will see 'country is already defined in myMethod()'
public class AlreadyDefinedSolution
{
public void myMethod()
{
String country;
String country="Bangladesh";// declared again
System.out.println(country);

}

}


Now the solution is to remove the declaration 'String' from the assignment statement.

public class AlreadyDefinedSolution
{
public void myMethod()
{
String country;
country="Bangladesh";//declaration removed
System.out.println(country);

}

}


    email this       edit

0 comments:

Post a Comment