This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java dunce is back...still can't figure this out

 
N. Gonzalez
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still attempting to increment the student id when I print out the instances of the student data using toString() method.

In my Student class file, I have a a static class variable:

this is in the constructor:

a getter for the class variable:

a getter for the instance variable:

and the 'studentid' field in my toString() method.


Here's the test file; it compiles, but all the IDs are '0'.

 
Madhumathy Viswanathan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the keyword static.When a variable is Static it maintains a common copy among all instances.Hence called class variable and not instance variable.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense that they would be 0, because you never set 'studentid', so they retain their implicit value (which is zero). Not seeing the full code, it would seem that 'studentid' is unnecessary, and you should just use 'studentID'. What purpose is served by having both of them? (As an aside, I would urge you not to have two fields with the same name that only differ in case; it's much too easy to make mistakes through typos).
 
N. Gonzalez
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was advised to have two variables, one of which should be static.

You really want this to be two variables. One to keep track of the current maximum assigned student id and one to keep track of the student id for that particular student. Tip: Think about which one of these needs to be static.


Here's my code for the Student class:

 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the following statement to your constructor:

studentid = studentID;

Your constructor would look like:



[ July 25, 2005: Message edited by: jaikiran pai ]
[ July 25, 2005: Message edited by: jaikiran pai ]
 
N. Gonzalez
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Jaikiran!

In the midst a number of making other changes, I recall doing this then removing it thinking it didn't work (so easy to make dumb mistakes when you're exhaused!)

It's a relief to know I wasn't far off. Thanks for peace of mind.
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to know, you got the expected output. Good day.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic