• Post Reply Bookmark Topic Watch Topic
  • New Topic

static block  RSS feed

 
abhinav sood
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Temp
{
public static void main(String... s)
{
int a[]=new int[10];
a[2]=1000;
System.out.println(a[2]);
}
}

here in the static block we are dynamically initialising the array so i wanted to know that how can we dynamically intialise the array in the static block and same is applicable for when we create the reference id inside the static block
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinav sood wrote:class Temp
{
public static void main(String... s)
{
int a[]=new int[10];
a[2]=1000;
System.out.println(a[2]);
}
}

here in the static block


"Static block"? I assume you mean the main() method. That's not a static block.

we are dynamically initialising the array so i wanted to know that how can we dynamically intialise the array in the static block and same is applicable for when we create the reference id inside the static block


Not sure what you're asking here, unless maybe the very first "static block" was just a typo, and you're asking how to initialize an array in a real static initializer block. If that's your question, it would be like this:


(Note that it's preferable to use int[] a rather than int a[], since it keeps the type ("array of int") part of the declaration together. It's also more common, so more people are used to reading it that way and will find it easier to read if you follow the convention.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!