• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Can not find variable

I am fairly new to Java, but I do have php programing experience. So, my question is

Code:
public myClass{
    public Object avgRGB(int img){
        // Some unrelated code to the problem
        Object avg = new Object(){
            public double red = 0.0;
            public double green = 0.0;
            public double blue = 0.0;
            public double bw = 0.0;
        };
        avg.bw = ((red / count) + (green / count) + (blue / count)) / 3;
    }
}

With, avg.bw = ... my Netbeans editor is giving me an error: can not find variable bw

Why not?
 
You need to make a new class like so:

Code:
public class MyObject {
            public double red = 0.0;
            public double green = 0.0;
            public double blue = 0.0;
            public double bw = 0.0;
}

and then in your code, you would do:
Code:
MyObject avg = new MyObject();
avg.bw = (red / count + green / count + blue / count) / 3;

EDIT: When creating an inline class like that in java, you can only access the members you create in the class itself.
 
Oops, I had a brain fart. There is no need to explicitly subclass Object. Whenever you create a class, it is implicitly a subclass of Object. So instead, you would just do:

Code:
public class MyObject {
            public double red = 0.0;
            public double green = 0.0;
            public double blue = 0.0;
            public double bw = 0.0;
}

I will edit my above post accodingly
 
Back
Top Bottom