question about static

J

Joona I Palaste

(e-mail address removed) scribbled the following:
I have seen code like the following and was wondering
what it means. I cannot understand the meaning of the
keyword static at this point. Further on, what does it mean
if a class is final? I know the keyword final just from the
context of variable declarations. And what is this Class.forname
thing (I dont understand the explanation from the API)?

The static at this point means that the following code is a static
initialiser block. It's run once when the class is first loaded into
the JVM, and then never run again.
A final class means a class that cannot be subclassed.
Class.forName is a method for generating Class objects (look it up
in the Java API docs) from class names.
public final class Blah extends HttpServlet {
 
S

steffen.mazanek

Hi.

I have seen code like the following and was wondering
what it means. I cannot understand the meaning of the
keyword static at this point. Further on, what does it mean
if a class is final? I know the keyword final just from the
context of variable declarations. And what is this Class.forname
thing (I dont understand the explanation from the API)?

public final class Blah extends HttpServlet {

static {
try {
Class.forname("org.postgresql.Driver");
} catch ...
}
...

}

Thank you and bye,
Steffen
 
D

Dale King

Joona I Palaste said:
(e-mail address removed) scribbled the following:


The static at this point means that the following code is a static
initialiser block. It's run once when the class is first loaded into
the JVM, and then never run again.


Extremely nit-picky comment, but it is run when it is loaded by each
ClassLoader. If the same class is loaded by multiple ClassLoaders it can run
more than once per JVM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top