Java class variable error?
public class class2 {
static int number3 = 86;
public static void Whale() {
static int number4 = 86;
}
}
why am I getting an error on line 5 and not on line # 2? Thank!
Because it Whale
is a method and you cannot define a static field inside a method. You can have a local one number4
like
public static void Whale() {
int number4 = 86;
or a static
, for example
static int number4 = 86;
public static void Whale() {
Finally, the class names of the conventions must begin with an uppercase letter ( CamelCase
) and method names with a lowercase letter ( CamelCase
).
You need to understand that java does not support static local variables, unlike C / C ++. But in some cases, Closures can help you achieve your goal.
Try using some sources for Closures -
- GoogleTechTalkByNeal Gafter ,
- AnotherBog ,
- java.dzone.com