Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fails to compile ?

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic