what is this special syntax?

J

Josef Garvi

public class MyClass {

{
// write some code here
}

}

When should I use those unnamed brackets, and how do they get called?

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
M

Michael Borgwardt

Josef said:
public class MyClass {

{
// write some code here
}

}

When should I use those unnamed brackets,

Never.

It's called an "instance initializer", btw.
See Java Language Specification, chapter 8.6
and how do they get called?

When an instance of the class is created. But that's
what constructors are for, which is what you should
use instead.
 
C

Carl Howells

Michael said:
When an instance of the class is created. But that's
what constructors are for, which is what you should
use instead.

Clearly, the one exception is the case where the class can't have a
constructor, because the class doesn't have a name. In the case of
anonymous classes, instance initializers are sometimes necessary.
 
J

Josef Garvi

Carl said:
Clearly, the one exception is the case where the class can't have a
constructor, because the class doesn't have a name. In the case of
anonymous classes, instance initializers are sometimes necessary.


Thanks guys. That was clear.


--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
T

Tim Jowers

Josef Garvi said:
Thanks guys. That was clear.

Also singleton using static class object (private constructor).

class C1 {

C1 oneInstance;

private C1() {}
{ // static init to create one C1
oneInstance = new C1(); // do other connection stuff in here one
time only
}

}

Also, there's some example as a way to overload default variables set
in constructors as inherited constructor is always called before code
in constructor of derived class. no init list as in C++.
 
A

Andrea Polci

Tim said:
Also singleton using static class object (private constructor).

class C1 {

C1 oneInstance;

private C1() {}
{ // static init to create one C1
oneInstance = new C1(); // do other connection stuff in here one
time only
}

}

Your code does nothing.
It should be:

class C1 {
static C1 oneInstance;

private C1() {}

static { // this is a static initializer. The one you wrote wasn't
oneInstance = new C1();
// do other connection stuff in here one time only
}

}
 
R

Ryan Stewart

Andrea Polci said:
Your code does nothing.
It should be:

class C1 {
static C1 oneInstance;

private C1() {}

static { // this is a static initializer. The one you wrote wasn't
oneInstance = new C1();
// do other connection stuff in here one time only
}

}
No, it should be this only if you want a class variable. The original way
creates an instance variable that's the same as your static member besides
the obvious.
 
T

Tony Morris

Ryan Stewart said:
No, it should be this only if you want a class variable. The original way
creates an instance variable that's the same as your static member besides
the obvious.

eh ?
The original post will cause a java.lang.StackOverflowError when the
constructor is ever called (assuming there is some omitted code) since it is
a recursive call without a base case.
The next post is one correct* way of providing a singleton object.
An alternative is called "lazy initialisation" where the first attempt to
access the instance is where it is created (rather than the provided code in
which the instance is created at class load time).
Whatever you are saying doesn't really make any sense and seems way out of
context.

*correct: "loosely speaking" - some purists will argue that there is more
required to ensure the integrity of the singleton but I care not to enter
that debate.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Ryan Stewart

Tony Morris said:
eh ?
The original post will cause a java.lang.StackOverflowError when the
constructor is ever called (assuming there is some omitted code) since it is
a recursive call without a base case.
The next post is one correct* way of providing a singleton object.
An alternative is called "lazy initialisation" where the first attempt to
access the instance is where it is created (rather than the provided code in
which the instance is created at class load time).
Whatever you are saying doesn't really make any sense and seems way out of
context.

*correct: "loosely speaking" - some purists will argue that there is more
required to ensure the integrity of the singleton but I care not to enter
that debate.

Ah, my bad. I didn't pay attention specifically to what they were doing with
it. I was just replying to the notion that the given code would do nothing,
which isn't true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top