static field initialised twice

T

Thomas Hawtin

Omega said:
class A {

private int id;
private static int sequence = 1;

public A () {
id = sequence;
sequence++;
}

}

Everythings is fine with the first object, it has id == 1, but when I
create second one, it has id == 0.
I notices that it happens in very starnge situation.

It makes it very difficult to find the cause of a problem if you haven't
posted the problem code. As you don't know where the problem is, it is
generally best to post a short, compilable piece of code that
demonstrates the problem. (And make sure you have compiled all the class
files from scratch, so that there cannot be any old versions hanging about.)
class B {

ArrayList<A> list;

public class B{
list = new ArrayList<A>();
}
}

I noticed that sequence changes value to 0 when I create new object of
class B. Why is it like this, and what can I do to stop it from
happening.

I can't reproduce the problem. Here's my modified, compilable version of
your code. Do you get the same thing?

import java.util.ArrayList;

class A {

private int id;
private static int sequence = 1;

public A () {
id = sequence;
sequence++;
}
public int getID() {
return id;
}
}
class B {

ArrayList<A> list;

//public class B{
public B() {
list = new ArrayList<A>();
}
}
class Driver {
public static void main(String[] args) {
A a1 = new A();
B bo = new B();
//B bi = bo.new B.B();
A a2 = new A();
System.err.println(a1.getID());
System.err.println(a2.getID());
}
}

Note that A.sequence is not correctly synchronised. Either put it in a
synchronised block (probably synchronised against A.class, or a static
lock field), or from 1.5 use java.util.concurrent.atomic.AtomicInteger.

Tom Hawtin
 
O

Omega

I have a problem with static field in my class.
I want every object of may class to have unique id, so I do something
like this:

class A {

private int id;
private static int sequence = 1;

public A () {
id = sequence;
sequence++;
}

}

Everythings is fine with the first object, it has id == 1, but when I
create second one, it has id == 0.
I notices that it happens in very starnge situation.

I have class like this:

class B {

ArrayList<A> list;

public class B{
list = new ArrayList<A>();
}
}

I noticed that sequence changes value to 0 when I create new object of
class B. Why is it like this, and what can I do to stop it from
happening.

Thanks in advance

Omega
 
M

Mark Thomas

Omega said:
I have a problem with static field in my class.
I want every object of may class to have unique id, so I do something
like this:

class A {

private int id;
private static int sequence = 1;

public A () {
id = sequence;
sequence++;
}

}

Everythings is fine with the first object, it has id == 1, but when I
create second one, it has id == 0.
No it doesn't. Adding a toString to your class A (so we can see what's
happening):

@Override
public String toString() {
return "A with id " + id;
}

Then testing it with:

public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
A a3 = new A();
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
}

You get:

A with id 1
A with id 2
A with id 3

Exactly as you should.
I notices that it happens in very starnge situation.

I have class like this:

class B {

ArrayList<A> list;

public class B{
list = new ArrayList<A>();
}
}

A strange class indeed, B with a nested class also called B! I think
you've gotten a constructor mixed up with an inner class.
I noticed that sequence changes value to 0 when I create new object of
class B. Why is it like this, and what can I do to stop it from
happening.

No it doesn't. Fixing that constructor:

import java.util.ArrayList;

class B {

ArrayList<A> list;

public B() {
list = new ArrayList<A>();
}
}

And modifying out test to:

public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
B b1 = new B();
A a3 = new A();
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
}

We still get:

A with id 1
A with id 2
A with id 3

So the sequence is not affected.
Thanks in advance

Omega
How are you getting your wrong impressions of the values? - you don't
show any output, or say how (you think) you know what's going on.

Mark
 
M

Mark Thomas

Thomas said:
Note that A.sequence is not correctly synchronised. Either put it in a
synchronised block (probably synchronised against A.class, or a static
lock field), or from 1.5 use java.util.concurrent.atomic.AtomicInteger.

Ooh! Thanks Thomas. I hadn't noticed AtomicInteger before. That's
really useful. So we'd have:

import java.util.concurrent.atomic.AtomicInteger;

class A {

private int id;
private static AtomicInteger sequence = new AtomicInteger(1);

public A () {
id = sequence.getAndIncrement();
}

}

Lovely!

Mark
 
O

Omega

Mark said:
Omega wrote:
How are you getting your wrong impressions of the values? - you don't
show any output, or say how (you think) you know what's going on.

Mark

I know whats going on like this:
class A {

private int id;
private static long sequence = 1;

public A(){
id = sequence;
sequence++;
System.out.println("id = "+id+" seq = "+sequence);
}
}

I cant post whole code here, because it's rather huge, there's whole gui,
plus I write this programm for a company and can't give it to everybody.
I can tell what's going on.

I have a program, in the begining there's one object of class B (the one
that holds list of A). When I create objects of class A, they all have
good id's that is: 1,2,3.... Then I can create second object of class B.
And then sequence is changed to 0. When I create objects A they have ids
0,1,2,... There are no threads in the programm, nothing unusual is going
on.

Omega
 
R

Robert Klemme

Omega said:
I know whats going on like this:
class A {

private int id;
private static long sequence = 1;

public A(){
id = sequence;
sequence++;
System.out.println("id = "+id+" seq = "+sequence);
}
}

I cant post whole code here, because it's rather huge, there's whole gui,
plus I write this programm for a company and can't give it to everybody.
I can tell what's going on.

I have a program, in the begining there's one object of class B (the one
that holds list of A). When I create objects of class A, they all have
good id's that is: 1,2,3.... Then I can create second object of class B.
And then sequence is changed to 0. When I create objects A they have ids
0,1,2,... There are no threads in the programm, nothing unusual is going
on.

You said UI - then you usually got at least two threads. Why don't you
just synchronize access to sequence and see what happens?

Btw, your sequence generator generates longs but your id is just int.
That's most likely a problem in itself.

Cheers

robert
 
C

Chris Smith

Omega said:
I cant post whole code here, because it's rather huge, there's whole gui,
plus I write this programm for a company and can't give it to everybody.
I can tell what's going on.

What you're saying is not possible. There must be something you haven't
mentioned. Are you using ClassLoaders anywhere? Do you have any code
that uses the "sequence" field other than in the constructor of A?

Unfortunately, if you can't be bothered to, or are legally prohibited
from, writing a reproducible demonstration of the problem, the answer is
going to be that you should find and hire a consultant. A consultant
will probably agree to sign an NDA if it's necessary.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top