What is temp and how it works (constructor)

So from processing tutorial https://www.youtube.com/watch?v=NrwaKOsplZk&list=PLzJbM9-DyOZyMZzVda3HaWviHqfPiYN7e&index=36 , there is a “temp” argument to define the diameter. my question is any relative explanation for temp and how would I use it properly. I didn’t get how to name one value with temp and how things get related: eg, from the video : why D is referring to temp D, why use letter D but not A B C? I am confused with the naming and using rules of “temp”.

To explain why see and Read links below :point_down:

thanks. but I watched the 8.1 object-oriented programming and class reference. It didn’t solve my problem:" what is temp "

A variable (parameter) is passed to the constructor.

Since it signifies the diameter it’s named D.

The variable is forgotten as soon as the constructor has been run; therefore the variable is temporary. This is abbreviated to temp. So it’s tempD for a temporary diameter.

Sometimes you see d_in (_in for incoming parameter) as a name or even d_ as a name.
It’s just important that the parameter has a different name than the name of the variables in the class (where we copy the temporary incoming variable into).

You did that correctly with your two bubbles.

1 Like

hello Chris,
thanks a lot.
I still have some more questions about Temp. (is this short for temporary?)
what if I want to define not only the diameters instead of color of the bubble. where you put the value and how will you name your temp and make sure there is a value pass to the constructor ?
thank you. and yes. I try to figure out the OOP and I almost figure out. haha

Yes, temp is short for temporary, not permanent

The name tempD is arbitrary. You can choose every name as a parameter as long as it’s not the same name as in the class variable.

You can separate parameters with comma.

Bubble (float tempD, color tempColor) {
   diameter = tempD;
   colBubble = tempColor; 
}

see class / Reference / Processing.org

By the way

you can also use the same name and use “this.”

Bubble (float diameter , color colBubble ) {
   this.diameter = diameter ;
   this.colBubble = colBubble ; 
}

Example

// Declare and construct two objects (h1, h2) from the class HLine 

HLine h1 = new HLine(20, 2.0); 
HLine h2 = new HLine(50, 2.5); 

void setup() {
  size(200, 200);
  frameRate(30);
}

void draw() { 
  background(204);
  h1.update(); 
  h2.update();
} 

// =======================================================================

class HLine { 
  float ypos, speed; 

  // constr 
  HLine (float y, float s) {  
    ypos = y; 
    speed = s;
  }// constr 

  void update() { 
    ypos += speed; 
    if (ypos > height) { 
      ypos = 0;
    } 
    line(0, ypos, width, ypos);
  } // method
} // class  
//