![]() |
Explain static
Code:
private static studentNum=0;Am I understanding this? |
Quote:
you access static methods only via the class name so, Student.getStudentNum() (assuming that the snippet you posted is part of the Student class) |
Thanks for the reply!
Quote:
Code:
Student bob = new Student();Student.getStudentNum();, is it Student.bob.getStudentNum();? |
It would be Student.getStudentNum(). Static variables are shared among ALL instances of the class so it doesn't require you to reference it via any particular instance (bob in your case), nor should you for clarity sake. Using Student.bob.getStudentNum() would work, but it will create a warning when you compile your code.
Just as a side note, in case you didn't already realize, that each Student class you instantiate will not have a unqiue studentNum. Every instance will have the same id (i.e. The total number of Student objects you have created). |
Quote:
Code:
class Student {Code:
Student bob = new Student(), |
Thanks.
This was a question for my summer assignment. (Dont worry, you did not do my homework for me) I understand it now. Thanks! |
| All times are GMT +1. The time now is 06:16 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.