0

If I was trying to increase the CRN of a number, I think that the correct way to do so would be to create an instance variable and a static variable

private int crn;
private static int nextCrn = 2015000;

and then to increment it, simply set them equal to each other in the constructor and increment,

public Course() {
    crn = nextCrn++;
}

For each time the constructor was called, it would increment the value 2015000 by 1.

Sorry for the confusion, I know that it will compile if you just increment the static variable,

i.e. crn = crn++

It's not the order of operation that is confusing me, but why do we need the instance variable in the first place to store the CRN for certain objects, like a commenter mentioned below. Why can't the static variable just have a different CRN for each object without an instance variable??

user180708
  • 407
  • 1
  • 4
  • 16
  • 2
    The static variable can be accessed across all objects while the instance variable can only be accessed from the specific object... – brso05 Mar 19 '15 at 17:44
  • Do you mean to ask `why doesn't it work to just increment the instance variable` instead of `why doesn't it work to just increment the static variable` ? crn in your example is the instance variable and not the static variable. – Chetan Kinger Mar 19 '15 at 17:44
  • if you do `crn = crn++;` it works. i am not sure what you asking – OPK Mar 19 '15 at 17:44
  • 1
    I'm assuming each instance should get a different CRN if you only use a static variable they will all have the same CRN. The static variable is used to keep track of the next available number while the instance variable is used to store the CRN for that particular object... – brso05 Mar 19 '15 at 17:46
  • @brso05, Yes, you understood my question exactly. I just thought that objects could access static and instance variables, so why add another instance variable? – user180708 Mar 19 '15 at 17:50
  • @user180708 The current object can access it's own instance variables but other objects of the same class can't. All objects of the same class can access static variables. The static variable is used by all objects created to get a unique number then it is stored in an instance variable of that particular object not accessible by other objects of the same class. I hope that makes sense it is kind of hard to find the right words... – brso05 Mar 19 '15 at 17:53

3 Answers3

1

I'm assuming each instance should get a different CRN if you only use a static variable they will all have the same CRN. The static variable is used to keep track of the next available number while the instance variable is used to store the CRN for that particular object.

The current object can access it's own instance variables but other objects of the same class can't. All objects of the same class can access static variables. The static variable is used by all objects created to get a unique number then it is stored in an instance variable of that particular object not accessible by other objects of the same class. I hope that makes sense it is kind of hard to find the right words.

brso05
  • 13,142
  • 2
  • 21
  • 40
  • I think this was what I was looking for, thanks for the detailed explanation! The only clarification I would need is when the next available number comes along, say 201500 is incremented by 1, then only the next object will have the instance variable store 201501 for it correct? The first one would remain untouched, and the others would remain blank. They're not all assigned 201501 and then overwritten with the next incrementation right? – user180708 Mar 19 '15 at 18:08
  • @user180708 that is correct the instance variables once assigned will not be affected by other instance variables of other objects or the static variable. Incrementing the static variable will not affect the instance variables that have already been assigned. – brso05 Mar 19 '15 at 18:24
0

Order of operations -- the postfix is done first and returns the original value. Hope that clarifies.

hd1
  • 33,938
  • 5
  • 80
  • 91
0

It's not the order of operation that is confusing me, but why do we need the instance variable in the first place to store the CRN for certain objects, like a commenter mentioned below. Why can't the static variable just have a different CRN for each object without an instance variable??

Because the static variable belongs to the class and is accessible by all instances, where the instance variable belongs to the instance and is unique for each object instance, hence the name "instance variable".

        //nextCrn = 201500
        Course c1 = new Course();
        //nextCrn = 201501, c1.crn = 201500
        Course c2 = new Course();
        //nextCrn = 201502, c1.crn = 201500, c2.crn = 201501
        Course c3 = new Course();
        //nextCrn = 201503, c1.crn = 201500, c2.crn = 201501, c3.crn = 201502
Erwin de Gier
  • 632
  • 7
  • 12