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]
# 1
You don't need a main method in order to compile.You do need a class or interface declaration, but when it's not present, you can assume it's there and that the specific classname and package are irrelevant.
jverda at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 2
Over 300 postings and you still don't know how to use the "Code Formatting Tags"....
camickra at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 3
sorry to post unformatted code. I really forgot it buddy.
skp71a at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 4
> . . . but when it's not present, you can assume it's there . . .Could you explain that a little further?
LazarusLonga at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 5

> > . . . 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).

jverda at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

jverda at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 7
I do not have any fundamental confusion buddy, I never did SCJP exam. Just to clarify I posted this question. Thanks.
skp71a at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...
# 8
> I do not have any fundamental confusionYour question suggests otherwise. Main has nothing to do with anything here, but obviously you think it's relevant.
jverda at 2007-7-13 0:26:26 > top of Java-index,Java Essentials,Java Programming...