I wonder if it's a big error (in small one class Java program) when I define variable in a class level rather that using the constructor? Can we do that?
The method body will be the same in both cases.
Many thanks!
eg.
public class test{
static int column1 = 0;
static int column2 = 1;
public static void main(String[] args){
// do something with variables, no return
}
/...../
}
You mean
public class Foo {
private String test = "Hello";
...
}
instead of
public class Foo {
private String test;
public Foo() {
test = "Hello";
}
}
I actually prefer the first method, it is much cleaner. If you know the value ahead of time and it's a constant, you should set the value as you define it. No reason not to from a technical standpoint, and it looks much cleaner.
Given the code sample you add, what you're doing is fine. One thing to consider is that your constants should be final. In your case they should likely be private static final int.
It doesn't make any difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With