wood burning stoves*
The moose likes Beginning Java and the fly likes problem in incrementing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in incrementing" Watch "problem in incrementing" New topic
Author

problem in incrementing

saad shaukat
Greenhorn

Joined: Mar 04, 2005
Posts: 1
I have a class Delivery. This class have DeliveryID. What I want is that when user create instance of delivery the id should be incremented itself. I am writing following code but it doesnt work. Plz fix this.


publc class Delivery{

private int DeliveryID = 1;

public Delivery{

DeliveryID++;
}
}

What i am trying to do is that when first object is created id should be 1 but when second object is created its id should be 2 and so on. Plz help me with this.

thanks.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
private int DeliveryID = 1; creates an instance variable, what you want is a class variable.

Plz fix it yourself
[ March 04, 2005: Message edited by: Ray Stojonic ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You'll need to have both a class (static) field and the instance field you already have. The static field will track the highest ID assigned so far while the instance field will hold the ID for each Delivery. In the constructor, you want to increment the shared ID and assign it to the instance member (or assign first to start from 1).

Also, the Java convention is for methods (except constructor) and variables to start with a lowercase letter and uppercase the first letter of each additional word -- justLikeThis.
 
 
subject: problem in incrementing