java - Variable has not been initialised -
public class color { private int red; private int green; private int blue; /** * turns color equivalent gray value. */ public void turngray() { int red = (int)(0.2126*red + 0.7152*green + 0.0722*blue); int green = red; int blue = red; } when try compile program returns variable red might not have been initialised. why dosn't code reassign red value on lhs of equation?
you re-declaring red,green , blue variables local variables in method, hiding instance variables having same names. local variable has no default value , can't accessed before initialized.
since have instance members same names, i'll assume meant use them :
public void turngray() { red = (int)(0.2126*red + 0.7152*green + 0.0722*blue); green = red; blue = red; } in code, instance variables used. now, hope initialize these variables in code didn't show us. otherwise they'll have default value of 0, , stay 0 when execute turngray.
Comments
Post a Comment