Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaration of static members  RSS feed

 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I need to know if

static { a[0]=2; } //IS this declaration wrong ??
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong? Not completely. Will it work? No.

What value do you think "a" has at that time? I'm not asking what values the elements have, I'm asking what value the array variable itself has.
 
anirudh jagithyala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be a "NullPointerException" or "ArrayIndexOutOfBoundsException"..........
Did you try to execute the same....?
 
anirudh jagithyala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If the array is given with some value before using it then it would work....
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anirudh jagithyala wrote:. . . Did you try to execute the same....?
That would have given you the answer to your question. And you have misunderstood the problem if you think there is anything specific to static members about this question.
 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnks guys ...
 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have jus another silly doubt ..

can we declare static a[0]=2; instead of static { a[0]=2; } ??


i'm not clear with the declarations...
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you go through the Java tutorials, because you obviously have some trouble with the language syntax.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
archu sweet wrote:. . . . can we declare static a[0]=2; instead of static { a[0]=2; } ? . . .
No.

That's not a declaration. And it won't compile. And the problem has nothing to do with the array being marked static.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
archu sweet wrote:


I need to know if

static { a[0]=2; } //IS this declaration wrong ??




For the instance variables if only type is mentioned then the default values are assigned.
For type long,int,short,char,byte the value is 0
For double,float it is 0.0
For boolean it is false
For reference type it is null.

In this case a is a reference type of class [I which is the class for array of int.
So the default value is null unless you assign it a value.

If you declare the array as int[] a={1,2,3}, then the array will have three elements at index 0,1,2. If you print a.length it will be 3.
With respect to the keyword static, it means that the array reference a is specific to the class and all the objects of the class share the same array. A variable marked as static is loaded into the memory once and remains until the program exits.

When you are assigning a value as a[0]=2; It will throw a NullPointerException if not initialized (that it is initialized by null). The code snippet static{} refers to the static block in a class. The static array a can be manipulated inside a static block or a a static method or a non-static method or block. The static block is loaded first into memory before the main is loaded. Any thing which is inside the static block gets executed first. If you don't write the main method then also it will run but after execution of the static block it will give an exception that cannot find main method. In order to get rid of the exception(when executing the program without main) the last statement of the static block should be System.exit(0), so that the exception not thrown.

For example

public class Foo{
static int[] a={1,2,3};

static{
a[0]=2;
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
System.exit(0);
}

}

The above code produces the same output as the below code

public class Foo{
static int[] a={1,2,3};

public static void main(String[] args){
a[0]=2;
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}

}
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation, but this bit is confusing:
kumarjit banerjee wrote: . . . if not initialized (that it is initialized by null). . . .
Since that is a field, when the object is created on the heap, a space is kept in memory for this reference to be put into. So you get 32 bits (on a 32-bit machine) or similar, where the location of that field will be put. These 32 or however many are filled with 0000s, before any initialisation, which is interpreted as null, etc. So at that point the field points to null and hasnot yet been initialised to anything. When it is initialised, it will contain the memory location of whatever it is initialised to. Of course you might initialise that field to 0, or even null.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as kumarjit banerjee and Campbell Ritchie pointed out, you need to initialize your array first.

You do not have to do this by loading it with any values.

Have a look at this:



I prefer this to loading values in the same line where I define the array, makes the code easy to read and thus easy to maintain.

Another difference is, that I defined my array final, the array itself cannot be changed.

A 'static' block can be a maximum of 65535 characters large and you can have a maximum of 64K static members.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!