1

I'm creating a class using java and it's a basic class to understand objects,methods etc.. anyways the class name is Student and it is supposed to assign a student ID to each newly created object. Student ID's start at 1000000 and increment by 1, so every new object should have the class assign a student ID, 10000001, 100000002 etc..

public class Student {

 private static long nextID=10000000;
 private long studentID;
 //etc..

 public Student (String name, long studentID, int count, double total score) {
        totalScore=0;
        count=0;
        this.name=name;
        studentID=nextID;
        nextID++;

 }

 public long getStudentID() {
       return nextID;`
 }

however when I create objects of this class the student ID keeps giving everyone the same student number, 10000000. please help

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
user2809437
  • 500
  • 1
  • 6
  • 21

3 Answers3

5

Your getStudentID function is returning the static counter instead of the instance variable.

public long getStudentID() {
    return nextID;
}

Should be:

public long getStudentID() {
    return studentID;
}

Also, in the constructor, you define a parameter called studentID, which hides the instance field of the same name, so when you do this:

studentID=nextID;

You are assigning a value to the parameter, which is then discarded when the method ends. You should remove the parameter, since you are tracking ID inside the class, you don't need to pass it in. You could also change it to this.studentID: the this explicitly refers to the instance field.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 2
    Was just about to add that there's no need for the studentID parameter. Good answer, I can never get to these easy questions fast enough –  Sep 24 '13 at 03:34
  • Now my output for studentID is 0.. `public Student (String name) { totalScore=0; count=0; this.name=name; this.studentID=nextID; nextID++; } public long getStudentID() { return studentID;}` – user2809437 Sep 24 '13 at 03:55
  • @Blorgbeard you were correct thank you, I had another constructor in my program that was handling my newly created object...java newbie thank you again and everyone else! – user2809437 Sep 24 '13 at 04:08
  • @user2809437 glad you got it sorted out! – Blorgbeard Sep 24 '13 at 04:15
0

Your getStudentID method is returning the wrong value. It should return the studentID field, not the static nextID field.

Like so:

public long getStudentID(){
    return this.studentID;
}
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
  • now getting 0 for my student id... 'private static long nextID=1000000; public Student {this.studentID=nextID; nextID++} public long getStudentID() {Return this.studentID;}' – user2809437 Sep 24 '13 at 03:59
  • @user2809437 See msangel's answer; that's what is causing your new issue. – Paul Richter Sep 24 '13 at 04:13
0

use this: this.studentID=nextID; instead of studentID=nextID;

msangel
  • 9,895
  • 3
  • 50
  • 69
  • This does not solve **ANY** type of issue he could possibly be having. – Josh M Sep 24 '13 at 03:41
  • @JoshM, yes, it solve. For first - without this OP not write to object field - he just assign new value to one of input params.(if you found, that studentID is not only field but also an parametr, so accoarding to visibility rules - he assign not field, but this parametr.). Other thing is, if studentId was static, it would cause a problem, so it garanted that we assign to a field of object but not to class. – msangel Sep 24 '13 at 03:47