Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

problem in incrementing

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic