Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > initialising statics

Reply
Thread Tools

initialising statics

 
 
Roedy Green
Guest
Posts: n/a
 
      01-22-2006
I probably never occurs to most people to initialise static variables
in main. After all, they are all automatically initialised to 0 or to
the values in the declarations.

All works fine if you run your program only from the command line. But
if someone starts automating, and calls your main repeatedly from
Java, the second calls will get whatever static values were left over
from the first invocation of main.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Andrew McDonagh
Guest
Posts: n/a
 
      01-22-2006
Roedy Green wrote:
> I probably never occurs to most people to initialise static variables
> in main. After all, they are all automatically initialised to 0 or to
> the values in the declarations.
>
> All works fine if you run your program only from the command line. But
> if someone starts automating, and calls your main repeatedly from
> Java, the second calls will get whatever static values were left over
> from the first invocation of main.


It does occur because its a dumb idea.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      01-22-2006
Roedy Green <(E-Mail Removed) > writes:
>All works fine if you run your program only from the command line. But
>if someone starts automating, and calls your main repeatedly from


What could be done from the point of view of the framework
to force proper initialisation of the main class for the
second time it is activated?

Something like "unloading" it and "reloading" it again via the
standard class loader (I do not know, if this is possible)?

 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      01-22-2006
Roedy Green wrote:
> I probably never occurs to most people to initialise static variables
> in main. After all, they are all automatically initialised to 0 or to
> the values in the declarations.
>
> All works fine if you run your program only from the command line. But
> if someone starts automating, and calls your main repeatedly from
> Java, the second calls will get whatever static values were left over
> from the first invocation of main.


One recommendation is to never have non-final static variables.
Following this eliminates the problem and will likely make the program
more object-oriented.

HTH,
Ray

--
This signature intentionally left blank.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-22-2006
On 22 Jan 2006 21:43:49 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram)
wrote, quoted or indirectly quoted someone who said :

> What could be done from the point of view of the framework
> to force proper initialisation of the main class for the
> second time it is activated?


If you were trying to automate badly behaved main code, you could use
a custom loader each time, but of course that partly defeats the
purpose. You want the speed of avoiding the program load each time. It
least it beats loading a new JVM with exec each time.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-22-2006
Andrew McDonagh wrote:
> Roedy Green wrote:
> > I probably never occurs to most people to initialise static variables
> > in main. After all, they are all automatically initialised to 0 or to
> > the values in the declarations.

>
> It does occur because its a dumb idea.


It's very unfortunate that your newsreader accidentally posted your
reply before you had a chance to replace this inflammatory statement
with a sound technical argument (and correct the amusing typo).

Perhaps you meant to say, for example, that initializing static
variables in main could be problematic if a different class's main()
were called, therefore reducing the modularity of the class in
question?

Luke

 
Reply With Quote
 
Silvio Bierman
Guest
Posts: n/a
 
      01-23-2006

"Raymond DeCampo" <(E-Mail Removed)> wrote in message
news:W1TAf.111709$(E-Mail Removed).. .
> Roedy Green wrote:
>> I probably never occurs to most people to initialise static variables
>> in main. After all, they are all automatically initialised to 0 or to
>> the values in the declarations.
>>
>> All works fine if you run your program only from the command line. But
>> if someone starts automating, and calls your main repeatedly from
>> Java, the second calls will get whatever static values were left over
>> from the first invocation of main.

>
> One recommendation is to never have non-final static variables. Following
> this eliminates the problem and will likely make the program more
> object-oriented.
>
> HTH,
> Ray
>
> --
> This signature intentionally left blank.


Statics are almost always a bad idea and anyone who uses them is lucky if
they never live to regret this poor choice. Less lucky people will
eventually get into situations where the
thought-of-as-universally-applicable property suddenly has to vary per
customer, country, user, session or whatever. Factoring out statics into
instance based constructs is very tedious. This can be addressed with
multiple classloaders but that is like cleaning dirt with fresh paint.

We made the same mistake in the past. Since we are still developing on the
same system and have been for about three years now we are still in the
process of taking out static constructs. Examples are: serving multiple
customers (each customer owns a database instance) from a single server
instance needing customer-level properties, having different languages for
each customer or possibly user, etc.
We implemented all these mechanisms using statics at first and have suffered
from that. When we could no longer afford to run a server instance per
customer and needed to merge them we where in trouble. Most things have been
repaired since at the expence of a lot of man hours. We have put a
death-penalty on using statics in new code except for static functions in
stateless utility classes, a la Arrays or Collections.

Regards,

Silvio Bierman


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      01-25-2006
Roedy Green <(E-Mail Removed) > wrote:
> I probably never occurs to most people to initialise static variables
> in main. After all, they are all automatically initialised to 0 or to
> the values in the declarations.
>
> All works fine if you run your program only from the command line. But
> if someone starts automating, and calls your main repeatedly from
> Java, the second calls will get whatever static values were left over
> from the first invocation of main.


If you call main method programmatically, you get what you get. Since
part of the contract of main is that it's called from a fresh JVM and
the process terminates when it's done. You violate the contract, and
there's no reason to expect your code to do anything in particular.

If you really want to make a program runnable programmatically as well
as an app, create a new method that's NOT main, and document its usage
in the normal way. Call it from main. Far less confusing.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      01-27-2006
Chris Smith wrote:
> Roedy Green <(E-Mail Removed) > wrote:
>
>>I probably never occurs to most people to initialise static variables
>>in main. After all, they are all automatically initialised to 0 or to
>>the values in the declarations.
>>
>>All works fine if you run your program only from the command line. But
>>if someone starts automating, and calls your main repeatedly from
>>Java, the second calls will get whatever static values were left over
>>from the first invocation of main.

>
>
> If you call main method programmatically, you get what you get. Since
> part of the contract of main is that it's called from a fresh JVM and
> the process terminates when it's done. You violate the contract, and
> there's no reason to expect your code to do anything in particular.
>
> If you really want to make a program runnable programmatically as well
> as an app, create a new method that's NOT main, and document its usage
> in the normal way. Call it from main. Far less confusing.


Or if you want to run an arbitrary Java program directly from another
Java program then do it the application server way: load the relevant
classes with their own ClassLoader. You'd then have to use reflection
to invoke the main() method, but it's considerably safer and more
robust. (Don't take that as absolute safety, however, and don't assume
robustness rivaling what you would achieve by giving the program its
own, dedicated, JVM.)

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lifetime of global statics vs. statics in functions Stuart MacMartin C++ 5 07-27-2005 04:19 PM
statics in inner classes Roedy Green Java 14 07-12-2005 12:59 AM
Statics and connections Jason ASP .Net 2 12-06-2004 03:27 PM
PIX 515 with statics and ACLs blocks dmz to outside access SuperIce Cisco 2 10-01-2004 05:11 PM
pix, vpn and statics P Cisco 1 08-06-2004 08:54 AM



Advertisments