Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-13-2008, 09:54 PM   PM User | #1
twodayslate
Senior Coder

 
twodayslate's Avatar
 
Join Date: Mar 2007
Location: VA
Posts: 1,042
Thanks: 67
Thanked 39 Times in 39 Posts
twodayslate is on a distinguished road
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!
twodayslate is offline   Reply With Quote
Old 08-14-2008, 06:44 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
shyam is offline   Reply With Quote
Old 08-15-2008, 02:19 AM   PM User | #3
twodayslate
Senior Coder

 
twodayslate's Avatar
 
Join Date: Mar 2007
Location: VA
Posts: 1,042
Thanks: 67
Thanked 39 Times in 39 Posts
twodayslate is on a distinguished road
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!
twodayslate is offline   Reply With Quote
Old 08-15-2008, 03:57 AM   PM User | #4
icm9768
New Coder

 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 2 Times in 2 Posts
icm9768 is an unknown quantity at this point
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..
icm9768 is offline   Reply With Quote
Users who have thanked icm9768 for this post:
twodayslate (08-15-2008)
Old 08-15-2008, 06:38 AM   PM User | #5
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
shyam is offline   Reply With Quote
Users who have thanked shyam for this post:
twodayslate (08-15-2008)
Old 08-15-2008, 10:13 PM   PM User | #6
twodayslate
Senior Coder

 
twodayslate's Avatar
 
Join Date: Mar 2007
Location: VA
Posts: 1,042
Thanks: 67
Thanked 39 Times in 39 Posts
twodayslate is on a distinguished road
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!
twodayslate is offline   Reply With Quote
Reply

Bookmarks

Tags
we have tags now?

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:06 PM.


Advertisement
Log in to turn off these ads.