Please answer this confusion.
In SCJP exam, if we see a question like below without main method, what to answer? For ex, this below example cannt be compiled.
String foo = "blue";
Boolean[] bar = new Boolean[1];
if (bar[0]) {
foo = "green";
}
but if you add main method, it will compile fine, only runtime exception will be thrown.
public class C {
public static void main(String[] args) {
String foo = "blue";
Boolean[] bar = new Boolean[1];
if (bar[0]) {
foo = "green";
}
}}
So, what to answer? Code cannot compile or exception is thrown? Thanks.
[615 byte] By [
skp71a] at [2007-11-27 9:56:19]

> > . . . but when it's not present, you can assume
> it's there . . .
>
> Could you explain that a little further?
String foo = "blue";
Boolean[] bar = new Boolean[1];
if (bar[0]) {
foo = "green";
}
By itself, the above cannot compile (even if there are no syntax errors in that code itself) because there's no surrounding class Blah { ... }. But if the question is something like "What happens with the above code? a) doesn't compile, b) runtime exception, ... etc. ..." I would assume that "doesn't compile" applies only if there's a syntax error in that code. I don't think they're testing to see if you recognize that you need the class declaration. They just ommitted to avoid clutter (is my guess).
I honestly don't know if we'd even see a question like that though, or if they'd always provide the class declaration just to avoid confusion about what the real question is.
The OP asked "What happens when there's no main method?" and seemed to think that a main method was required in order to compile. Then he added a main method AND a class declaration, so I think the OP has some fundamental confusion about the pieces of a Java class or program, and even the distinction between a class and a program.