The Java Program: Primitive.java

  1 class Primitive {
  2 
  3    private final int r,g,b;
  4 
  5    Primitive (final int red, final int green, final int blue) {
  6       r = red; g = green; b = blue;
  7    }
  8 
  9    // This method does not work as might be expected
 10    public void getRGBColor (int red, int green, int blue) {
 11       red = r; green = g; blue = b;
 12       // Changes to local variables are lost when method returns.
 13    }
 14 
 15    // This method is not a satisfactory alternative.
 16    int [] getRGBColor () {
 17       final int [] rgb = {r,g,b};  // (BTW.  Array aggregates only in initializers.)
 18       return (rgb);
 19    }
 20 
 21    public static void main (String[] args) {
 22       final Primitive p = new Primitive (200,225,250);
 23       int red, green, blue;
 24       /*
 25         The following are not legal as the variables red, green, and blue
 26         have not been initialized!
 27 
 28       p.getRGBColor (red,green,blue);
 29       System.out.println ("red="+red+", green="+green+", blue="+blue);
 30 
 31         Initizalizing them does not solve the underlying problem that
 32         "getRGBColor" cannot assign to the variables.
 33 
 34       */
 35       red=0; green=0; blue=0;
 36       p.getRGBColor (red,green,blue);
 37       System.out.println ("red="+red+", green="+green+", blue="+blue);
 38    }
 39 
 40 
 41 }