aspose file tools*
The moose likes Beginning Java and the fly likes Java dunce is back...still can't figure this out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java dunce is back...still can Watch "Java dunce is back...still can New topic
Author

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

N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
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

Joined: Jun 01, 2005
Posts: 6
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.


Madhumathy
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
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).


Ping & DNS - my free Android networking tools app
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
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

Joined: Jul 20, 2005
Posts: 10266
    
168

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 ]

[My Blog] [JavaRanch Journal]
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
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

Joined: Jul 20, 2005
Posts: 10266
    
168

Nice to know, you got the expected output. Good day.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java dunce is back...still can't figure this out