*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock  exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mock  exam" Watch "mock  exam" New topic
Author

mock exam

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
public class MyClass
{
final int i ;
public static void main(String[] arguments)
{
System.out.println(new MyClass().i);
}
}
-----------------------------
Fails to compile saying that i is not initialized. Why is it so when i is an instance variable?
Thanks


Sunita<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
class MyClass
{
public static void main (String args[])
{
String s1[] = new String[5];
String str = s1[0].toUpperCase();
System.out.println(str);
}
}
----------------------------------------------
this gives null pointer exception.Why is it so?
Thanks
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi,
An instance final variable has to be initialized before being used inside a constructor or an instance initializer.
When constructing your String array, it was initiliazied to null and therefore you're trying to use null in a case where an object is required. That triggers the NullPointerException
Hope this helps


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks Shisho.. am able to understand now...
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Good for you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: mock exam
 
Similar Threads
Can a class be singleton after implements Runnable?
this
Package Accessiblty problem
Number of Objects eligible for Garbage Collection
getInstance() Is that really a method()