public class Main {
public static final Logger LOGGER = Logger.getLogger(Main.class.getName());
static {
try {
LOGGER.addHandler(new FileHandler("errors.log",true));
}
catch(IOException ex) {
LOGGER.log(Level.WARNING,ex.toString(),ex);
}
}
...
I'm wondering what is this nameless static function about.
I never saw anything like this in java (which I'm currently learning).
What is it for ?
When is it typically used ?
When is this gonna be executed in the program ?
it is a method accessible from outside the class where it is defined (public), it is a static method (static), it does not return any result (return type is void), and. it has a parameter of type array of strings (see Unit 7).
Yes you can call a static method without the class name.
In Java, a static method may use the keyword void as its return type, to indicate that it has no return value.
A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.
That is called a static block and will only be executed once during initialization. Also, if there are more than one static initialization blocks, the run time guarantees that they will be called in the order they appear in the source code.
Here is a pretty good explanation with some example code a. https://www.geeksforgeeks.org/g-fact-79/
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