Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Executing code when a class loads

Thread view: 
Josef Garvi - 15 Apr 2004 19:30 GMT
Is it possible to execute code when a class is loaded from a package?
Like a sort of static constructor?

Signature

Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty

Yoyoma_2 - 15 Apr 2004 19:35 GMT
> Is it possible to execute code when a class is loaded from a package?
> Like a sort of static constructor?

You mean you have a Class myClass and you want to execute it?
you just have to do Object instance = myClass.newInstance();
Bryce (Work) - 15 Apr 2004 19:41 GMT
>Is it possible to execute code when a class is loaded from a package?
>Like a sort of static constructor?

Yep. Static initializers.

class Something {
 static {
   // code here will execute when class is first loaded
 }
}

--
now with more cowbell
Josef Garvi - 15 Apr 2004 19:46 GMT
> Yep. Static initializers.
>
[quoted text clipped - 3 lines]
>   }
> }

Thanks.

Signature

Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty

Dale King - 15 Apr 2004 19:44 GMT
> Is it possible to execute code when a class is loaded from a package?
> Like a sort of static constructor?

Yes. You are referring to a static initializer. It looks like this:

class MyClass
{
   static
   {
       System.out.println( "Called when class is loaded" );
   }
}

--
 Dale King
 Blog: http://nobleware.homedns.org/Blog
Roedy Green - 15 Apr 2004 22:24 GMT
>Is it possible to execute code when a class is loaded from a package?
>Like a sort of static constructor?

see http://mindprod.com/jgloss/initialisation.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.