As the title says, what exactly is the difference between
public static String myString = \"Hello World!\";
and
public stat
For your example, there is no difference. But as you can see,
public static String myString = "Hello World!";
can only accept an expression to initialize the variable. However, in a static initializer (JLS 8.7), any number of statements may be executed. E.g. it's possible to do this:
static
{
myString = "Hello";
myString += " ";
myString += "World";
}
For your example, there's obviously no need to do that, but it's possible for the initialization of a variable to take more than an expression, perhaps many statements, so Java made static initializers.
Well, in your first example the variable is declared and initialized on the same line. In your second the variable is first declared, then initialized. In the second case you could have any number of other static variable and block initializations occur before getting to the initialization block in question. Consider this case:
public static String myString = "Hello World!";
public static String yourString = myString;
static {
System.out.println(myString);
System.out.println(yourString);
}
vs:
public static String myString ;
public static String yourString = myString;
static {
myString = "Hello World";
}
static {
System.out.println(myString);
System.out.println(yourString);
}
Output from the first example:
Hello World Hello World
Output from the second example:
Hello World null
Difference between static initializer block and regular static initialization.
In case of variable initialization both are same.
But If we want to connect with database only once or any operation you want to load once. Then write the code in static block because its execute only once when first class load no matter how many objects of that type you create.
EDIT :
You can also construct a similar block:
{
// Do Something...
}
Example:
public class Demo {
static{
System.out.println("Static");
}
{
System.out.println("Non-static block");
}
public static void main(String[] args) {
Demo demo = new Demo();
Demo demo2 = new Demo();
}
}
Output:
Static
Non-static block
Non-static block
The static {...} block gives you the opportunity to do more than you would be able to do in the field declaration.
For example, you can fill in some details of a map:
private static final Map<String, String> data = new HashMap<String, String>();
static {
data.put("A", "Hello");
data.put("B", "There");
data.put("C", "You");
}
Sometimes you may also need to get data (from a file, database, etc) before you can instantiate:
public class Foo {
private static final Person person;
static {
InputStream personData = Foo.class.getResourceAsStream("something.txt");
person = new Person(personData);
}
...
}
To continue with what Scott Stanchfield wrote, you can use the Collections.unmodifiableXXX()
methods for safety, though libraries like Google Guava may make that less necessary. Consider:
public static final Map<String, String> CAPITALS;
static {
Map<String, String> map = new HashMap<>(); //Java 7.
map.put("NY", "Albany");
map.put("MD", "Annapolis");
map.put("VA", "Richmond");
map.put("CT", "Hartford");
// 46 more states
CAPITALS = Collections.unmodifiableMap(map);
}
Of course, having a 52-line static block may be disorienting, and so you might instead take the static block and turn it into a static method.
public static final Map<String, String> CAPITALS = capitals();
private static Map<String, String> capitals() {
Map<String, String> map = new HashMap<>(); //Java 7.
map.put("NY", "Albany");
map.put("MD", "Annapolis");
map.put("VA", "Richmond");
map.put("CT", "Hartford");
// 46 more states
return Collections.unmodifiableMap(map);
}
The difference is a matter of style. You might instead just work with a database table.
Generally a static variable's value is shared between all instances (or the non-instance) of the Class it is defined in where a static block is a section of code that gets executed when Class is first loaded. Functionally There is no difference.