aspose file tools*
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
Author

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 :
test.java:26: illegal start of type
new test();
Why does this fail to compile ?
Anonymous
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="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</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].
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fails to compile ?
 
Similar Threads
static constructor
exceptions thrown from initializer blocks
a static inner class question
Initialisers
name conflict