Constructor execution

B

bagarow

Hi

In the following code fragment:

class Cups {
static int cupCount = 10;
Cups () {
System.out.println ("Cups");
// Would like to create a array of cup objects here
}
}

public class Cuppas2 {
public static void main (String[] args) {
System.out.println("Inside main");
System.out.println("Cup Count is " + Cups.cupCount);
}
}

The constructor for class Cups does not get executed when the static
field Cups.cupCount is referenced in the main method print statement.

If in the initialization of the class Cups, I wanted to use a non
simple code fragment e.g. creating an array of objects with a for loop,
is there a way to do that in the client code without calling new
Cuppas2()?

Thanks
Bob
 
V

Vova Reznik

Hi

In the following code fragment:

class Cups {
static int cupCount = 10;
Cups () {
System.out.println ("Cups");
// Would like to create a array of cup objects here
}
}

public class Cuppas2 {
public static void main (String[] args) {
System.out.println("Inside main");
System.out.println("Cup Count is " + Cups.cupCount);
}
}

The constructor for class Cups does not get executed when the static
field Cups.cupCount is referenced in the main method print statement.

If in the initialization of the class Cups, I wanted to use a non
simple code fragment e.g. creating an array of objects with a for loop,
is there a way to do that in the client code without calling new
Cuppas2()?

Thanks
Bob
use
static{
}
 
E

Eric Sosman

Hi

In the following code fragment:

class Cups {
static int cupCount = 10;
Cups () {
System.out.println ("Cups");
// Would like to create a array of cup objects here

Go right ahead; nothing's stopping you.
}
}

public class Cuppas2 {
public static void main (String[] args) {
System.out.println("Inside main");
System.out.println("Cup Count is " + Cups.cupCount);
}
}

The constructor for class Cups does not get executed when the static
field Cups.cupCount is referenced in the main method print statement.

No, of course not. The job of a Cups constructor is
to initialize a newly-created Cups object. You never ask
for any Cups objects to be created, so no Cups constructor
ever executes, or ever has any reason to execute.
If in the initialization of the class Cups, I wanted to use a non
simple code fragment e.g. creating an array of objects with a for loop,
is there a way to do that in the client code without calling new
Cuppas2()?

Creating a new Cuppas2 object won't do anything about
Cups. The Cuppas2 constructor (yes, there is one, even
though you can't see it) calls the no-argument constructor
of its superclass (Object, in this case) and then performs
any Cuppas2-specific initialization. Since there isn't any
Cuppas2-specific initialization to perform, the constructor
just returns after the Object constructor returns to it. At
no point does this do anything about the completely unrelated
class Cups: it doesn't make any new Cups objects, and in fact
it doesn't even cause the Cups class itself to be loaded.

Perhaps (*perhaps*) what you're struggling toward is the
static initialization block, something like

class Cups {
static int cupCount = 10;

static Cups[] cups = new Cups[cupCount];
static {
for (int i = 0; i < cups.length; ++i)
cups = new Cups();
}

Cups() {
... as before ...
}
}

.... but I confess I'm not at all clear as to your intent.
 
B

bagarow

Eric said:
Hi

In the following code fragment:

class Cups {
static int cupCount = 10;
Cups () {
System.out.println ("Cups");
// Would like to create a array of cup objects here

Go right ahead; nothing's stopping you.
}
}

public class Cuppas2 {
public static void main (String[] args) {
System.out.println("Inside main");
System.out.println("Cup Count is " + Cups.cupCount);
}
}

The constructor for class Cups does not get executed when the static
field Cups.cupCount is referenced in the main method print statement.

No, of course not. The job of a Cups constructor is
to initialize a newly-created Cups object. You never ask
for any Cups objects to be created, so no Cups constructor
ever executes, or ever has any reason to execute.
If in the initialization of the class Cups, I wanted to use a non
simple code fragment e.g. creating an array of objects with a for loop,
is there a way to do that in the client code without calling new
Cuppas2()?

Creating a new Cuppas2 object won't do anything about
Cups. The Cuppas2 constructor (yes, there is one, even
though you can't see it) calls the no-argument constructor
of its superclass (Object, in this case) and then performs
any Cuppas2-specific initialization. Since there isn't any
Cuppas2-specific initialization to perform, the constructor
just returns after the Object constructor returns to it. At
no point does this do anything about the completely unrelated
class Cups: it doesn't make any new Cups objects, and in fact
it doesn't even cause the Cups class itself to be loaded.

Perhaps (*perhaps*) what you're struggling toward is the
static initialization block, something like

class Cups {
static int cupCount = 10;

static Cups[] cups = new Cups[cupCount];
static {
for (int i = 0; i < cups.length; ++i)
cups = new Cups();
}

Cups() {
... as before ...
}
}

... but I confess I'm not at all clear as to your intent.


I had a typo in my original email. Instead of saying new Cuppas2(), I
should have said I wanted the initialization of class Cups to create a
pool of Cup objects without having to call new Cups() in the client
code but instead by the invocation of the Cups class via referencing
the static field Cups.cupCount . So the static block you all have
suggested in Cups does the trick.

Thanks much
Bob
 
B

bagarow

Eric said:
Hi

In the following code fragment:

class Cups {
static int cupCount = 10;
Cups () {
System.out.println ("Cups");
// Would like to create a array of cup objects here

Go right ahead; nothing's stopping you.
}
}

public class Cuppas2 {
public static void main (String[] args) {
System.out.println("Inside main");
System.out.println("Cup Count is " + Cups.cupCount);
}
}

The constructor for class Cups does not get executed when the static
field Cups.cupCount is referenced in the main method print statement.

No, of course not. The job of a Cups constructor is
to initialize a newly-created Cups object. You never ask
for any Cups objects to be created, so no Cups constructor
ever executes, or ever has any reason to execute.
If in the initialization of the class Cups, I wanted to use a non
simple code fragment e.g. creating an array of objects with a for loop,
is there a way to do that in the client code without calling new
Cuppas2()?

Creating a new Cuppas2 object won't do anything about
Cups. The Cuppas2 constructor (yes, there is one, even
though you can't see it) calls the no-argument constructor
of its superclass (Object, in this case) and then performs
any Cuppas2-specific initialization. Since there isn't any
Cuppas2-specific initialization to perform, the constructor
just returns after the Object constructor returns to it. At
no point does this do anything about the completely unrelated
class Cups: it doesn't make any new Cups objects, and in fact
it doesn't even cause the Cups class itself to be loaded.

Perhaps (*perhaps*) what you're struggling toward is the
static initialization block, something like

class Cups {
static int cupCount = 10;

static Cups[] cups = new Cups[cupCount];
static {
for (int i = 0; i < cups.length; ++i)
cups = new Cups();
}

Cups() {
... as before ...
}
}

... but I confess I'm not at all clear as to your intent.


I had a typo in my original email. Instead of saying new Cuppas2(), I
should have said I wanted the initialization of class Cups to create a
pool of Cup objects without having to call new Cups() in the client
code but instead by the invocation of the Cups class via referencing
the static field Cups.cupCount . So the static block you all have
suggested in Cups does the trick.

Thanks much
Bob
 
T

Thomas Hawtin

Eric said:
static Cups[] cups = new Cups[cupCount];
static {
for (int i = 0; i < cups.length; ++i)
cups = new Cups();
}


IIRC, Effective Java suggests doing something like:

static Cups[] cups = createCups();

private static Cups[] createCups() {
Cups[] cups = new Cups[cupCount];
for (int i=0; i<cups.length; ++i) {
cups = new Cups();
}
return cups;
}

I prefer the former, but at least be aware of the possibility of the latter.

Tom Hawtin
 

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,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top