Java Native Interface - static something

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maria Gaitani

    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


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

    static {
    System.loadLibr ary("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


  • Ryan Stewart

    #2
    Re: Java Native Interface - static something

    "Maria Gaitani" <M.Gaitani@warw ick.ac.uk> wrote in message
    news:4020d196$0 $15423$fa0fcedb @lovejoy.zen.co .uk...[color=blue]
    > 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.loadLibr ary("Hello"); }
    >
    > I understand it is calling the library ... what I don't get is the[/color]
    structure[color=blue]
    > static { }.
    > I'd be grateful if you could explain what this structure is ...
    >
    > Thank you
    > Maria[/color]

    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.


    Comment

    • Jared Dykstra

      #3
      Re: Java Native Interface - static something

      "Maria Gaitani" <M.Gaitani@warw ick.ac.uk> wrote in message news:<4020d196$ 0$15423$fa0fced [email protected] o.uk>...[color=blue]
      > 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.loadLibr ary("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[/color]


      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:



      ---
      Jared Dykstra

      Comment

      • Maria Gaitani

        #4
        what's the difference with the constructor

        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


        Comment

        • Ryan Stewart

          #5
          Re: what's the difference with the constructor

          "Maria Gaitani" <M.Gaitani@warw ick.ac.uk> wrote in message
          news:40216402$0 $27378$fa0fcedb @lovejoy.zen.co .uk...[color=blue]
          > Thank you both very much for your reply !
          >
          > May I ask why is there a need for such a block since the same thing[/color]
          happens[color=blue]
          > if that block of code would be executed again only once if it was in the
          > constructor of the class.
          >
          > Thanks again,
          > Maria[/color]

          For one thing, you don't have to instantiate the object for the static block
          to be executed.


          Comment

          • chris

            #6
            Re: what's the difference with the constructor

            Maria Gaitani wrote:
            [color=blue]
            > 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.[/color]

            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 [email protected] net.be
            /k/ Embedded Java Solutions

            Comment

            • Maria Gaitani

              #7
              Re: what's the difference with the constructor

              Thank you very much for the very concise and helpful reply.
              Maria


              Comment

              Working...