difference btwn "does not compile" and "complie time error"
lucy hu
Ranch Hand
Joined: Aug 21, 2000
Posts: 60
posted
0
hi, guys, this is a problem from Mock test of JonnHunt What will be the result of compiling the following code: public class Test { public static void main (String args []) { int age; age = age + 1; System.out.println("The age is " + age); } } A.Compiles and runs with no output B.Compiles and runs printing out The age is 1 C.Compiles but generates a runtime error D.Does not compile E.Compiles but generates a compile time error could anybody tell me why the answer is D, not E? thanks, Lucy
Hi Lucy - I'm guessing that E is a misleading answer, i.e., there's no such situation, so E can never be true. Possibly the author means to say "The code compiles but generates a compile-time warning." I see this situation when I use deprecated methods -- there may be other situations that do this too, but that's a guess. ----------------- Michael Ernest, co-author of:
[This message has been edited by Michael Ernest (edited December 03, 2000).]
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
rahul attaluri
Greenhorn
Joined: Nov 22, 2000
Posts: 3
posted
0
hi, u r using a local variable,i.so when u try to compile, it says variable i may not have been initialized.u have to initialize local variables explicitly.initialize age to 0 and it'll work. whereas for class variables they r automatically intialized once declared(eg:int variable to 0,boolean to false etc). arrays too r intialized once declared. bye. rahul.
Sahir Shah
Ranch Hand
Joined: Nov 05, 2000
Posts: 158
posted
0
<HTML> Maybe by "generating a compile time error" he means he means "this will drive the compiler nutz ". And it is possible. Try this out and see what happens. <pre> public class Outer{ public static class Inner{ public static int a = 1; } public static void main(String[] args) { System.out.println(new Outer().Inner.a); } } </pre> Cheers Sahir </HTML> [This message has been edited by Sahir Shah (edited December 04, 2000).] Ajith removed the Java Steaming Coffee Cup logo (the official Java technology logo). Please note that this logo is is reserved for Sun's use ONLY and cannot be reproduced on product packaging, marketing materials. web pages, etc
[This message has been edited by Ajith Kallambella (edited December 05, 2000).]