52251

Java static initializers and reflection

Using java reflection I noticed that all classes run their static constructors when targeted by reflection. Is there any way that I can force these static constructors to return or not run them at all?

Answer1:

No. The static initializer is invoked as soon as the class is loaded by the classloader and initialized.

Answer2:

I add my answer, because I think what Bozho answers is too short now ;)

Reflection can initialize a class, only loading a class does not initialize it. Requesting the value from a static field for example does ensure the static initializer will have been called before you get the actual value. Only getting the Field usually does not. So if your reflective code causes static initializer to be executed and you really need to do it exactly like that, then there is no way to prevent those initializers from being called. If you want to avoid them getting called you have to change what you do by Reflection.

Recommend

  • No dhtreader in java.library.path
  • ClassNotFoundException during Deserialization of a just-serializaed class
  • java.lang.IncompatibleClassChangeError
  • Paperclip in Production with Unicorn: uninitialized constant Paperclip (NameError)
  • Using Recaptcha with EPiServer XForms
  • Grails Packaging and Naming Conventions
  • Hadoop NoSuchMethodError apache.commons.cli
  • how to create trigger to do concatenation of fields
  • How do I get width and height of my terminal with ioctl?
  • C# List.Add not adding properly
  • How to initialize global variable at each class invocation?
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • How can I get the maximum number of OpenMP threads that may be created during the whole execution of
  • Displaying pdf files using the PDFKit interface
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • What do I do with this error when I run tests in rails?
  • MRO with multiple inheritance in python
  • Swift Initialization Rule Confusion
  • Debugging VB6 Code From Visual Studio 2010
  • converting text file into xml using php?
  • PHP buffered output depending on server setting?
  • Ajax Loaded meta Tags
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Cannot Parse HTML Data Using Android / JSOUP
  • File upload with ng-file-upload throwing error
  • WPF Applying a trigger on binding failure
  • Free memory of cv::Mat loaded using FileStorage API
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • How to push additional view controllers onto NavigationController but keep the TabBar?