I’m trying the static and non-static methods and fields.
I tried to compile this:
class main{
public int a=10;
static int b=0;
public static void main(String[] args){
b+=1; //here i can do anything with static fields.
}
}
class bla {
void nn(){
main.a+=1; //why here not? the method is non-static and the field "main.a" too. Why?
}
}
and the compiler returns me:
try.java:10: non-static variable a cannot be referenced from a static context
but why? The method and the field “a” are both non-static!
You are attempting to access
ain a static manner. You will first need to instantiatemainto accessa.Also, for readability, you should capitalize the names of Classes, and camel case your instance variables.