Sunday, 13 September 2009

Java Certification Question 0094

Which option most fully describes will happen when you attempt to compile and run the following code

public class MyAr{

public static void main(String argv[]) {
MyAr m = new MyAr();
m.amethod();
}

public void amethod(){
static int i;
System.out.println(i);
}
}

1) Compilation and output of the value 0
2) Compile time error because i has not been initialized
3) Compilation and output of null
4) Compile time error



Answer)

4) Compile time error

An error will be caused by attempting to define an integer as static within a method. The lifetime of a field within a method is the duration of the running of the method. A static field exists once only for the class. An approach like this does work with Visual Basic.

No comments: