473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Java Native Interface - static something

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
6 3446
"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
"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
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
"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
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
Thank you very much for the very concise and helpful reply.
Maria
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
1
by: Cris | last post by:
My question in a nutshell: Can a C header file be converted into a C# interface? Details for why I want this below... I am trying to use C# in lieu of C/C++ to build some libraries that java...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
2
by: Andrea | last post by:
Hi evrybody ! Does anybody know how to call a java class from a COM component ? Any suggestion ? Thanks Andrea
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.