Question:
JAVA: Number for each object in a class?
Nightwing
2009-05-20 05:10:49 UTC
I have a description of a class which i need to write skeletal declarations for.

If there is a bunch of objects assigned to the class, and there is a number (named TNumber) assigned to each one, which is one more than that of the previous object (it goes up 1 for each object). What declaration would satisfy this? Thanks in advance.
Five answers:
Professor
2009-05-20 05:41:41 UTC
BOTH the above are incorrect, you will FAIL your assignment if you use them. This is the classic misuse of static - you will get unpredictable results in a multiuser environment.



One user might get 0;1;6;7 and another user might get 2;3;4;5;8 and this is not what you wanted!



/////////////////////////////

// This is C# but it will give you a reliable TNumber for all objects

/////////////////////////////

public class KeepTrack

{

private List Register = new List();



public int TNumber(Object find)

{

var result = this.Register.Find(hashCode => hashCode == find.GetHashCode());



if (result == -1)

{

result = this.Register.Count;

this.Register.Add( find.GetHashCode() );

}



return result;

}

}

/////////////////////////////
doug
2009-05-20 12:42:39 UTC
You could have a static int member and every time the constructor is called you can increment the static int and set the value of TNumber to the static int.



private static int objNumber = 0;

private TNumber;



yourClass(){



TNumber = ++objNumber;



}



edit:

and someone made a good point about this not being thread safe.



If that’s a requirement, you will have to take a different approach.



edit 2:



making this thread safe is easier than I thought it would be.



check out this package: java.util.concurrent.atomic



class AtomicIdGenerator {

private static AtomicLong id = new AtomicLong(0);

public static long nextId() {

long next = id.incrementAndGet();

return next;

}

}
shrawan_iitg
2009-05-20 12:35:32 UTC
http://javaexplain.com/



public class exobject {



public static void main(String arg[]) throws Exception {



CountedClass ob = new CountedClass();

System.out.println("object1 : "+ ob.TNumber);

CountedClass ob1 = new CountedClass();

System.out.println("object2 : "+ ob1.TNumber);

CountedClass ob2 = new CountedClass();

System.out.println("object3 : "+ ob2.TNumber);



}

}



class CountedClass

{

static int Count = 0; // use static

int TNumber = Count++;

}





OUTPUT of the Code:

object1 : 0

object2 : 1

object3 : 2



http://javaexplain.com/

Learn JAVA programming by Examples code. Latest JAVA/J2EE, JavaScript,XML, Puzzles interviews Question Answer
anonymous
2009-05-21 08:48:43 UTC
I agree that you need to to have protection from multiple threads BUT for early day examples you will probably be running from the main() method so only using one thread anyway, especially if you are taking lessons where you are probably being taught how to increment a counter rather than worry about the multiple thread environment. I cant imagine many early day assignments giving taking marks off for not considering multiple threads at this stage. Believe me I write trading interfaces where thread access is VERY important..



Here's how to do it which will give you protection from multiple threads as well as working fine for a single thread.



http://computerprogrammingacademy.com/javablog/2009/05/21/java-synchronized-staic-counter/



One not however is that using the synchronised method WILL slow down your code so if you are not using more than one thread, leave the synchronised block out...



Hope this helps...
SC
2009-05-20 12:20:23 UTC
It would have to be something like:



class CountedClass

{

public static int TCount = 0;

public int TNumber = TCount++;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...