Java initializer block syntax

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Java initializer block syntax

Damien Cassou-3
Hi,

I discovered that the following syntax is valid Java code:

class A {

  {
      /* any java code here */
  }

}


This code will be executed just before the constructor. I haven't
found any use for this syntax, even in the case of subclasses.

Does anyone have any idea or is it just useless?

--
Damien Cassou

Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Damien Cassou-3
The following code results in:

"
In the main()
In staticMethod
In the Main initializer block
In the Main constructor
In the B initializer block
In the B constructor
"

public class Main {

        String var = staticMethod();
       
        public static String staticMethod() {
                System.out.println("In staticMethod");
                return "";
        }
       
        {
                System.out.println("In the Main initializer block");
        }
       
        public Main() {
                System.out.println("In the Main constructor");
        }
       
        public static void main(String[] args) {
                B b;
               
                System.out.println("In the main()");
                b = new B();
        }
}

public class B extends Main{
        public B() {
                super();
                System.out.println("In the B constructor");
        }
        {
                System.out.println("In the B initializer block");
        }
       
       
}

--
Damien Cassou

Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Michael Haupt-3
In reply to this post by Damien Cassou-3
Hi Damien,

On 10/9/07, Damien Cassou <[hidden email]> wrote:
> Does anyone have any idea or is it just useless?

one possible use for this is to have common initialisation code that
is evaluated before *any* constructor.

Best,

Michael

Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Bergel, Alexandre
>> Does anyone have any idea or is it just useless?
>
> one possible use for this is to have common initialisation code that
> is evaluated before *any* constructor.

Exactly! Since a super constructor will _always_ be executed before  
your constructor.

class A{
        A() { /* CODE A*/ }
}
class B extends A {
        B() { /* CODE B */ }
}

CODE B will always be executed before CODE A. The construction you  
described is a way to short it out.

Cheers,
Alexandre
--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.




Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Klaus D. Witzel
In reply to this post by Damien Cassou-3
On Tue, 09 Oct 2007 17:23:54 +0200, Damien Cassou wrote:

> Hi,
>
> I discovered that the following syntax is valid Java code:
>
> class A {
>
>   {
>       /* any java code here */
         /* suffixed with .dll and .so for illustration: */
        System.loadLibrary("my.dll");System.load("your.so");
        /* otherwise some of your native methods [might] fail */

>   }
>
> }
>
>
> This code will be executed just before the constructor. I haven't
> found any use for this syntax, even in the case of subclasses.
>
> Does anyone have any idea or is it just useless?
>



Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Philippe Marschall
2007/10/9, Klaus D. Witzel <[hidden email]>:

> On Tue, 09 Oct 2007 17:23:54 +0200, Damien Cassou wrote:
>
> > Hi,
> >
> > I discovered that the following syntax is valid Java code:
> >
> > class A {
> >
> >   {
> >       /* any java code here */
>          /* suffixed with .dll and .so for illustration: */
>         System.loadLibrary("my.dll");System.load("your.so");
>         /* otherwise some of your native methods [might] fail */
> >   }

Wouldn't you put this code normally in a static initializer (without
the .so and .dll)?

Cheers
Philippe

> >
> > }
> >
> >
> > This code will be executed just before the constructor. I haven't
> > found any use for this syntax, even in the case of subclasses.
> >
> > Does anyone have any idea or is it just useless?
> >
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

[OT] Re: Java initializer block syntax

Bert Freudenberg
On Oct 9, 2007, at 18:07 , Philippe Marschall wrote:
>
> Wouldn't you put this code normally in a static initializer (without
> the .so and .dll)?

Isn't this the wrong mailing list for such questions?

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: [OT] Re: Java initializer block syntax

Ignacio Vivona-2
class initialization?

On 10/9/07, Bert Freudenberg <[hidden email]> wrote:
On Oct 9, 2007, at 18:07 , Philippe Marschall wrote:
>
> Wouldn't you put this code normally in a static initializer (without
> the .so and .dll)?

Isn't this the wrong mailing list for such questions?

- Bert -






Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Jason Johnson-5
In reply to this post by Damien Cassou-3
Well, that's basically instance variable declarations and methods to
initialize them as I recall.  That is, I don't think you can call a
"side effects only" function there.

But in any case, what brings it up?  Are you wishing Smalltalk had
this?  Because personally I like the way Smalltalk world works as
apposed to the C++/Java world of magic functions getting implicitly
called in orders one must memorize.

In Smalltalk there are (normally) functions getting called on creation
but you don't have to memorize it, it's in the code where you can read
it.

On 10/9/07, Damien Cassou <[hidden email]> wrote:

> Hi,
>
> I discovered that the following syntax is valid Java code:
>
> class A {
>
>   {
>       /* any java code here */
>   }
>
> }
>
>
> This code will be executed just before the constructor. I haven't
> found any use for this syntax, even in the case of subclasses.
>
> Does anyone have any idea or is it just useless?
>
> --
> Damien Cassou
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Java initializer block syntax

Damien Cassou-3
Hi,

thanks to all. I absolutely do not want Smalltalk to have this
"feature" :-). I was just wondering.

If I asked here, it's because I noticed that people who know Java very
well are often Smalltalkers :-).

Bye

2007/10/9, Jason Johnson <[hidden email]>:

> Well, that's basically instance variable declarations and methods to
> initialize them as I recall.  That is, I don't think you can call a
> "side effects only" function there.
>
> But in any case, what brings it up?  Are you wishing Smalltalk had
> this?  Because personally I like the way Smalltalk world works as
> apposed to the C++/Java world of magic functions getting implicitly
> called in orders one must memorize.
>
> In Smalltalk there are (normally) functions getting called on creation
> but you don't have to memorize it, it's in the code where you can read
> it.
>
> On 10/9/07, Damien Cassou <[hidden email]> wrote:
> > Hi,
> >
> > I discovered that the following syntax is valid Java code:
> >
> > class A {
> >
> >   {
> >       /* any java code here */
> >   }
> >
> > }
> >
> >
> > This code will be executed just before the constructor. I haven't
> > found any use for this syntax, even in the case of subclasses.
> >
> > Does anyone have any idea or is it just useless?
> >
> > --
> > Damien Cassou
> >
> >
>
>


--
Damien Cassou