We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Why constructor is not needed in class 'MyPixel'
Page Index Toggle Pages: 1
Why constructor is not needed in class 'MyPixel'? (Read 1252 times)
Why constructor is not needed in class 'MyPixel'?
May 25th, 2010, 11:30pm
 
I am learning 'class' now and I came up with a problem in class 'MyPixel', it seems works fine if I just ignore to create the constructor.

Code:
//MyPixel p;
MyScreen s = new MyScreen(600,600);

void setup(){
 size(600,600);
 background(0);
 //p = new MyPixel(10,20,200);

 //p.plot();
 //for(int i = 0; i < s.pixelGrid.length; i++){
 s.plot();
 //}
}

class MyScreen{
 //members
 MyPixel[] pixelGrid;

 //constructor
 MyScreen(int xgrid, int ygrid){
   pixelGrid = new MyPixel[xgrid*ygrid];

   for(int i = 0; i < pixelGrid.length; i++){
     pixelGrid[i] = new MyPixel();
     pixelGrid[i].x = i % xgrid;
     pixelGrid[i].y = i / ygrid;
     pixelGrid[i].c = color((int)random(255));
   }
 }

 //method
 void plot(){
   for(int i = 0; i < pixelGrid.length; i++){
     pixelGrid[i].plot();
   }    
 }

}

class MyPixel{

 //members
 float x,y;
 color c;
 
 //constructor
 MyPixel(float _x, float _y, color _c){
   x = _x;
   y = _y;
   c = _c;  
 }



 //method
 public void plot(){
   stroke(c);
   //point(x,y);
  ellipse(x, y, 5,5);
 }



}
Re: Why constructor is not needed in class 'MyPixel'?
Reply #1 - May 25th, 2010, 11:39pm
 
Constructor here is just a convenient way of setting several parameters at once: instead of one line of instantiation and three lines of assignment, you can make only one line of instantiation with parameters.
Moreover, the constructor can be used to compute internal variables, load stuff, etc.
And last, in classical Java, you usually hide the class variables (fields) by making them private, so the constructor is much more useful (there can be setters too, methods to set these fields).
Last note: you actually call a constructor, that is created automatically by the compiler, and that does nothing (beside creating the object).
Re: Why constructor is not needed in class 'MyPixel'?
Reply #2 - May 26th, 2010, 5:28am
 
Perhaps I'm repeating what PhiLho (why are you squared now?) has already said here...

You call a constructor with no arguments:

Quote:
pixelGrid[i] = new MyPixel();


It would seem that if you don't declare a constructor within your class then a default constructor (that takes no arguments) is enabled.  However, when you do declare a constructor then this default constructor is not enabled; which would be why you've probably been getting an error along the lines of "The constructor ...MyPixel() is undefined".

Since the constructor you've defined takes arguments you must pass these arguments when you call the constructor.  Alternatively you can create multiple constructors for your class that take different sets of arguments:

Code:
class MyPixel{

 //members
 float x,y;
 color c;
 
 //constructor
 // takes no arguments
 MyPixel() {
   // But maybe your object will break if no default values are
   // set for the object properties...
   // So perhaps set default values for variables here?
 }
 // Takes three arguments...
 MyPixel(float _x, float _y, color _c){
   x = _x;
   y = _y;
   c = _c;  
 }

// ... snip ...
}
Re: Why constructor is not needed in class 'MyPixel'?
Reply #3 - May 26th, 2010, 7:59am
 
Thanks, it is very clear and really helpful.
Page Index Toggle Pages: 1