• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice to know, you got the expected output. Good day.
 
What's a year in metric? Do you know this metric stuff tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic