• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Exception

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
CAn anybody explain me the following snippets of code:

1. public class X{
static{
Throw new NullPointerException()
}
}
Ans: The code will not compile...
My guess is since it's a static initialization block which is not having proper initializing statement so it should throw "ExceptionInitilizerError"

2. class.forName("java.lang.String");'

Ans: It will not throw any Exception..(???Please explain)


Thanks & Regards
Swapnil
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static initializer has to complete normally.

Also the forName method in Class returns a reference to a Class object
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith, I understood...first one was not too tough but second one was a bit deep....wasn't it??

Actually I was roaming around the classes that we generally use(searching forName method) and never assumed that SCJP 1.5 can ask Questions of so much depth in API.Does the real exam also ask Questions from these type of Classes???


Regards
Swapnil
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's one that does not complete succesfully:


But it does compile!

Edited: 1 == 1 replaced by equivalent true
[ June 24, 2006: Message edited by: Barry Gaunt ]
 
Don't listen to Steve. Just read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic