question that always bothered me

K

Karl Hungus

What is the difference between setting the value of a field in a class
outside the constructor (and any method), from setting it inside. eg:


private int age = 12;


vs.


private int age;
public myclass() {

age = 12;
}
 
P

P.Hill

Tony said:

Sadly the example doesn't mention at all when (in-line) initialization occurs.
I might extend it a bit and try something like:

abstract class Eggs {
{
System.out.println(
"At the end of superclass Egg's initializers: x = " + getX() );
}
abstract void init();
abstract int getX();

Eggs() {
System.out.println( "At the start of superclass Egg's constructor");
init();
System.out.println(
"At the end of superclass Egg's constructor x = " + getX() );
}
}

public class Toast extends Eggs {
private int x = 2;
{
System.out.println(
"At the end of the subclass Toast's initializers x = " + x );
}

public int getX() {
return x;
}

Toast() {
x = 3;
System.out.println(
"At the end of the subclass Toast's constructor: x = " + x );
}

public void init() {
System.out.println("In subclass Toast.init(): x = " + x);
}

public static void main(String[] args) {
Toast t = new Toast();
System.out.println(
"After constructing the subclass Toast: x = " + t.getX() );
}
}

which results in:

At the end of superclass Egg's initializers: x = 0
At the start of superclass Egg's constructor
In subclass Toast.init(): x = 0
At the end of superclass Egg's constructor x = 0
At the end of the subclass Toast's initializers x = 2
At the end of the subclass Toast's constructor: x = 3
After constructing the subclass Toast: x = 3

Which demonstrates init memory, super init, super c'tor, sub init, sub c'tor

I might also put a comment on the init block (bare { } ) explaining what it is.

-Paul
 
P

P.Hill

Roedy said:

Roedy,

That is a very nice summary, but I have a suggestion
for improvement of that nice table, because

"inline
e.g. int a = 1; [...] Interleaved in textual order with static {} or instance{}."

static init' blocks are done "long" before in-line member
initialization and init blocks, so static {} should NOT be mentioned
in that sentence and

"static init block e.g. static {a = 1;}"
should say: right after the class is loaded by the class loader

The following demostrates the behavior.

public class StaticInitDemo {
static {
System.out.println(
"**Before staticI declaration it can't be referenced " );
}
{
System.out.println( "Before memberI declaration it can't be referenced " );
}

int memberI = 3;
static int staticI = 5;

static {
System.out.println(
"**After in-line staticI declaration: staticI is " + staticI );
}
{
System.out.println( "After memberI declaration: memberI = " + memberI );
}

void incI() {
++memberI;
}

int getI() {
return memberI;
}

public static void main(String[] args) {
System.out.println( "Top of main...");
StaticInitDemo i1 = new StaticInitDemo();
System.out.println( "i1.memberI after creation is " + i1.getI() );
StaticInitDemo i2 = new StaticInitDemo();
System.out.println( "i2.memberI after creation is " + i2.getI() );
i2.incI();
System.out.println( "i2.memberI after incI() is " + i2.getI() );
}
}

results in:
**Before staticI declaration it can't be referenced
**After in-line staticI declaration: staticI is 5
Top of main...
Before memberI declaration it can't be referenced
After memberI declaration: memberI = 3
i1.memberI after creation is 3
Before memberI declaration it can't be referenced
After memberI declaration: memberI = 3
i2.memberI after creation is 3
i2.memberI after incI() is 4

Notice how all static initialization happened before
"Top of main..."

Of course interleaving static and member init' is really ugly form
but it demos the right behavior.

-Paul
 
I

iamfractal

P.Hill said:
Roedy Green wrote:


int memberI = 3;
static int staticI = 5;

System.out.println( "i1.memberI after creation is " + i1.getI() );
StaticInitDemo i2 = new StaticInitDemo();
System.out.println( "i2.memberI after creation is " + i2.getI() );
i2.incI();
System.out.println( "i2.memberI after incI() is " + i2.getI() );
}
}

results in:
**Before staticI declaration it can't be referenced
**After in-line staticI declaration: staticI is 5
Top of main...
Before memberI declaration it can't be referenced
After memberI declaration: memberI = 3
i1.memberI after creation is 3
Before memberI declaration it can't be referenced
After memberI declaration: memberI = 3
i2.memberI after creation is 3
i2.memberI after incI() is 4

Notice how all static initialization happened before
"Top of main..."

Of course interleaving static and member init' is really ugly form
but it demos the right behavior.

-Paul


Excellent examples all, which only goes to show the value of Bloch's
book, "Effective Java," namely that you must NEVER call an overridable
method from a constructor.

As a matter of personal taste (and we all know how valuable that is,
right?), I think you should assign a variable as soon as possible. If
you know it should be 12 on declaration, then assign assign it on
declaration.

www.EdmundKirwan.com
 
P

P.Hill

Excellent examples all, which only goes to show the value of Bloch's
book, "Effective Java," namely that you must NEVER call an overridable
method from a constructor.

To argue with myself; I ONCE had a good use for an overridable
method in a constructor.

The initialization sequence just couldn't be done:
1. call super c'tor
2. do more init'ing.
but it seemed MO' BETTE' to have extension then "super()",
(or at least setting something before doing the super)
so I invented a protected init() method which was called
in the right place in the super c'tor and commented how/when
it was supposed to be overridden.
It was definitely one of those cases where it was "an
exception that proves the rule", in the sense it demonstarted
that the rule is overwhelmingly a good one and the exceptions
rank as curiosities.
As a matter of personal taste (and we all know how valuable that is,
right?), I think you should assign a variable as soon as possible.

Good rule of thumb.

-Paul
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top