File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Fails to compile ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Fails to compile ?" Watch "Fails to compile ?" New topic

Fails to compile ?

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
public class test
public static void main( String[] args )
System.out.println(" Hello ");
class B extends test{
new test();
Compile time error : illegal start of type
new test();
Why does this fail to compile ?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi! You can't just type new test(); in the B class.
What shall it look for. Maybe put it in a method and call it from the test class.
or supply a main method in the B class

// Hope this helps
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
Hi, Angela, i thought in class level, we should either declare something, or initialise something, but cannot merely executely something. But in block level(or method level), we can just merely execute some code.
Correct me if i am wrong.

Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
Try my mock exam¹ at my homepage.

Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="" target="_blank" rel="nofollow"></a>, Java resource, mock exam, forum
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
The reason is that anonymous class or arrays can't be left unreferenced when they are members of class and with arrays, this is true, even when they are local members.Try assigning the reference to a reference variable in class B ,the program works fine.
[Please Correct if wrong].
I agree. Here's the link:
subject: Fails to compile ?
It's not a secret anymore!