Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which part is wrong in this following fragment

 
uday krishna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.lang.reflect.Constructor;
Constructor con;
con=new Constructor(Class.forName("Object"));
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second line of the fragment should be the one which is illegal. Are we not supposed to have class declaration here?
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this thread to the Intermediate section where it will get the attention it deserves. This topic is not covered in the SCPJ2 exam... Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the code is identified as a "fragment", it's impossible to say just what's wrong with it - it depends on how it's used. If the fragment is used inside a class declaration, then the first line is illegal - imports cannot be inside classes. The third line is also illegal unless it's inside a method or static initializer. If it's outside a class, then the second and third lines are illegal.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic