By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

Java Native Interface - static something

P: n/a
Hi!

I am trying to programme some java native interface but I'm still in the
process of research.
I've seen examples such as this one
http://java.sun.com/docs/books/tutor...tep/step1.html

but I don't understand the third and fourth line of the code of the above
example:

static {
System.loadLibrary("Hello"); }

I understand it is calling the library ... what I don't get is the structure
static { }.
I'd be grateful if you could explain what this structure is ...

Thank you
Maria
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Maria Gaitani" <M.*******@warwick.ac.uk> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
Hi!

I am trying to programme some java native interface but I'm still in the
process of research.
I've seen examples such as this one
http://java.sun.com/docs/books/tutor...tep/step1.html

but I don't understand the third and fourth line of the code of the above
example:

static {
System.loadLibrary("Hello"); }

I understand it is calling the library ... what I don't get is the structure static { }.
I'd be grateful if you could explain what this structure is ...

Thank you
Maria


In general, the first time a class or one of its fields is referenced, its
static initializers are executed. I don't know where to find an exact
definition of what happens when.
Jul 17 '05 #2

P: n/a
"Maria Gaitani" <M.*******@warwick.ac.uk> wrote in message news:<40***********************@lovejoy.zen.co.uk> ...
Hi!

I am trying to programme some java native interface but I'm still in the
process of research.
I've seen examples such as this one
http://java.sun.com/docs/books/tutor...tep/step1.html

but I don't understand the third and fourth line of the code of the above
example:

static {
System.loadLibrary("Hello"); }

I understand it is calling the library ... what I don't get is the structure
static { }.
I'd be grateful if you could explain what this structure is ...

Thank you
Maria

That is a static initializer block--in this case used to load the
native code library as part of the System, exactly once when the class
is instantiated. You can use static initialization blocks within
classes to do all kinds of stuff. A simple google search will give
you lots of additional information:

http://www.google.com/search?q=java+static+initializer

---
Jared Dykstra
http://www.bork.org/~jared
Jul 17 '05 #3

P: n/a
Thank you both very much for your reply !

May I ask why is there a need for such a block since the same thing happens
if that block of code would be executed again only once if it was in the
constructor of the class.

Thanks again,
Maria
Jul 17 '05 #4

P: n/a
"Maria Gaitani" <M.*******@warwick.ac.uk> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
Thank you both very much for your reply !

May I ask why is there a need for such a block since the same thing happens if that block of code would be executed again only once if it was in the
constructor of the class.

Thanks again,
Maria


For one thing, you don't have to instantiate the object for the static block
to be executed.
Jul 17 '05 #5

P: n/a
Maria Gaitani wrote:
Thank you both very much for your reply !

May I ask why is there a need for such a block since the same thing
happens if that block of code would be executed again only once if it was
in the constructor of the class.


No, it would be executed once for every instance of that class that was
created.
HelloWorld hw1 = new HelloWorld();
HelloWorld hw2 = new HelloWorld();
Now you've called loadLibrary() twice. Contrariwise, if you never create an
instance of the class then the library is never loaded.

Some classes are what we call "utility" classes: they have only static
members, and there is no way to create an instance of the class(*). For
such classes, a static{} clause is really the only place to do any
initialisation work that may be required.

Some classes are what we call "singletons": they are designed so that only
one instance can be created, and a reference to this instance can be
obtained by calling a static method (often called getInstance()). In this
case it may be a question of taste whether initialisation code is contained
in a static{} clause or in the constructor.

Most classes however are designed so that many instances can be created. In
this case code which should only be executed once should normally go in a
static{} clause (or be called from there), and code that should be called
for each instance belongs in (or gets called from) a constructor.

(*) If a class has a default constructor so:
private Foo() {}
there is no way to construct an instance from outside the class.

HTH
--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #6

P: n/a
Thank you very much for the very concise and helpful reply.
Maria
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.