I need some explanation about this question,
options in answer say :class SomeClass{
String s1 = "green mile"; // 0
public void generateReport( int n ){
String local; // 1
if( n > 0 ) local = "good"; //2
System.out.println( s1+" = " + local ); //3
} }
1). Insert else "bad" (Which is fine as my understanding)
2). Move line 1 and place it after 0, as it local variable inside the method. (???)
If i compile this code, by adding the main method in the above code like this
This code works fine even we are not making automatic variable "local" as instance variable.class SomeClass{
String s1 = "green mile"; // 0
public void generateReport( int n ){
String local; // 1
if( n > 0 ) local = "good"; //2
else local = "bad";
System.out.println( s1+" = " + local ); //3
}
public static void main(String args[]) {
SomeClass aa = new SomeClass(); // for using the generateReport() method
aa.generateReport(5);
}}
Isn't the second option redundant ... as this question can be answered completely with 1st option only.
Please explain if I am missing something with respect to this question.
Regards
/Umair