Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

Declaration of static members

 
Ranch Hand
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I need to know if

static { a[0]=2; } //IS this declaration wrong ??
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the array is given with some value before using it then it would work....
 
Marshal
Posts: 75705
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnks guys ...
 
archu sweet
Ranch Hand
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75705
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75705
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I AM MIGHTY! Especially when I hold 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