I need to declare private, public and static variables in java, is there any specific order to follow in declaration of these variables
public class Test
{
private String name;
public String address;
private static final String DR_NO = "1-44-54/4";
}
Is there any specification to declare these variables?
No, you can declare fields in whichever order you want. Same for methods, constructors, and inner classes.
The only thing to watch out for is that: static fields will be initialized before instance fields (of course), but static fields and instance fields will be initialized in the order you declared them.
So if you had this:
class A {
static String b = "hello ";
static String a = b + " world";
static void print() {System.out.println(a);}
}
then calling print
prints "hello world", but if you had this:
class A {
static String a = b + " world";
static String b = "hello ";
static void print() {System.out.println(a);}
}
then it would print "null world", as b
contained the value null
when a
was initialized.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.