• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hi can anybody solve this problem....

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anybody tell me what is the output of this code.

public class Test{
static int[] a;
static{
a[0]=2;
}
psvm(-){

Test t=new Test();

}
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried compiling it?? what did you get the output??
 
naveen maddala
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

Thanks for your reply,

No i haven't tried it.
Do you have any idea?

Regards,
Naveen.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You dont have JDK installed in your machine??
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will give a RuntimeException (ExceptionInitializerError).
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveen,

"Hi can anybody solve this problem" is not a meaningful subjectline.
 
naveen maddala
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can you please let me know in detail of that exception.

Bye,
Naveen.
 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static int[] a;

The static array is just declared not initialized . And it is a instance int[] variable. So the default value it gets is null.

in static block we are trying to assign a value to the array which is null. Actually it gives NullPOinterException.

As it is in static block it gives ArrayInitializerError. If anything goes wrong in static block we will get this error.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess thats because the static variable a[] is intialised before created

a[0]= 2 // tries to intialise the int array which is not yet created

int[] a // just declaration

It has be created using 'new' keyword = new int[5];
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balu Sadhasivam wrote:I guess thats because the static variable a[] is intialised before created

a[0]= 2 // tries to intialise the int array which is not yet created

int[] a // just declaration

It has be created using 'new' keyword = new int[5];


Technically, the variable a (an array variable) is never initialized in that code. You are attempting to assign a value to an element of a non-existent array object.
reply
    Bookmark Topic Watch Topic
  • New Topic