Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Explain static

  1. #1
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts

    Explain static

    Code:
    private static studentNum=0;
    public Student() {
    studentNum++;
    }
    public static int getStudentNum() { return studentNum; }
    public int getStudentNum is only static so it can return the static StudentNum correct? To call it you would say Student.bob.getStudentNum();

    Am I understanding this?
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by twodayslate View Post
    public int getStudentNum is only static so it can return the static StudentNum correct? To call it you would say Student.bob.getStudentNum();?
    who is bob?

    you access static methods only via the class name so, Student.getStudentNum() (assuming that the snippet you posted is part of the Student class)
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Thanks for the reply!
    Quote Originally Posted by shyam View Post
    who is bob?

    you access static methods only via the class name so, Student.getStudentNum() (assuming that the snippet you posted is part of the Student class)
    Code:
    Student bob = new Student();
    So to get bob's student number what do you call? It can't be Student.getStudentNum();, is it Student.bob.getStudentNum();?
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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).
    Last edited by icm9768; 08-15-2008 at 03:59 AM.

  • Users who have thanked icm9768 for this post:

    twodayslate (08-15-2008)

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by twodayslate View Post
    Code:
    Student bob = new Student();
    So to get bob's student number what do you call? It can't be Student.getStudentNum();, is it Student.bob.getStudentNum();?
    as icm9768 has rightly pointed out there is no bob's student number...if you want to store bob's student number you should store it in a member variable sorta like this
    Code:
    class Student {
      private static studentNum=0;
      private myNumber;
      public Student() {
        this.myNumber = studentNum++;
      }
      public static int getStudentNum() { 
        return studentNum; 
      }
      public int getNumber() {
        return myNumber;
      }
    }
    Code:
    Student bob   = new Student(),
            dylan = new Student();
    
    Student.getStudentNum(); // returns 2
    bob.getNumber(); // gives 0
    dylan.getNumber(); // gives 1
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    twodayslate (08-15-2008)

  • #6
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Thanks.
    This was a question for my summer assignment. (Dont worry, you did not do my homework for me)
    I understand it now. Thanks!
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •