Initializer block not called when static method called

public class Initializer {
     Initializer(){
          System.out.println("Constructor called");
               System.out.println("CLASS INITIALIZED");
     static void method(){
          System.out.println("Static method called");
     public static void main(String[] args) {
          Initializer.method();
From the JLS
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
[b]
But when i call the static method , if the class is initialized shouldnt the initializer block be called, it is not called, why.

Perhaps running something like this will add a little more colour?:
public class Initializer {
    static {
        System.out.println("First static initializer");
        System.out.println("First instance initializer");
    Initializer() {
        System.out.println("Constructor");
    static {
        System.out.println("Second static initializer");
        System.out.println("Second instance initializer");
    static void staticMethod() {
        System.out.println("staticMethod");
    void instanceMethod() {
        System.out.println("instanceMethod");
    public static void main(String[] args) {
        System.out.println("main");
        staticMethod();
        new Initializer().instanceMethod();
}

Similar Messages

  • Calling a static method when starting the server???

    Hi,
    i wanted to call a static method of a java class,
    whenever i start the server.
    plz give some input on this.
    thanks and regards
    siva

    Siva -- Can you be more specific as to the problem you are trying to solve (why do you want to call this static method,.)
    The reason I ask is that there are some ways to make this happen but they may have limitations
    that won't work for you. For instance, you can create a servlet that calls your class' static method and then make the
    servlet be loaded on startup.
    Thanks -- Jeff

  • How to call a static method in a class if I have just the object?

    Hello. I have an abstract class A where I have a static method blah(). I have 2 classes that extend class A called B and C. In both classes I override method blah(). I have an array with objects of type B and C.
    For every instance object of the array, I'm trying to call the static method in the corresponding class. For objects of type B I want to call blah() method in B class and for objects of type C I want to call blah() method in C class. I know it's possible to call a static method with the name of the object, too, but for some reason (?) it calls blah() method in class A if I try this.
    So my question is: how do I code this? I guess I need to cast to the class name and then call the method with the class name, but I couldn't do it. I tried to use getClass() method to get the class name and it works, but I didn't know what to do from here...
    So any help would be appreciated. Thank you.

    As somebody already said, to get the behavior you
    want, make the methods non-static.You all asked me why I need that method to be
    static... I'm not surprised to hear this question
    because I asked all my friends before posting here,
    and all of them asked me this... It's because some
    complicated reasons, I doubt it.
    the application I'm writing is
    quite big...Irrelevant.
    Umm... So what you're saying is there is no way to do
    this with that method being static? The behavior you describe cannot be obtained with only static methods in Java. You'd have to explicitly determine the class and then explicitly call the correct class' method.

  • JNI static method call fails for the 6th time

    Hello,
    I have a JNI Method which calls the static method which gets reference to singleton class(getReference()), JVM crashes.
    what might be the problem?
    is it due to insufficient memory or any other reason?
    Here is my code.
    eScannerClass = gEnv->FindClass("com/elvista/jscaner/EScanner");
    eScannerContructId = gEnv->GetStaticMethodID(eScannerClass,"getReference","()Lcom/elvista/jscaner/EScanner;");
    eScannerUpdateMethodId = gEnv->GetMethodID(eScannerClass,"updateScanStatus","(Lcom/elvista/jscaner/EScanEvent;)V");
    eScannerObjectRef = gEnv->NewObject(eScannerClass,eScannerContructId);Thanx for any help on this.

    Hi,
    the eScannerContructId is refering a static method, not a constructor. Therefore you must not use gEnv->NewObject, which is only allowed for constructors. Instead you have to use gEnv->CallStaticObjectMethod to call getReference().
    Martin

  • How to call a static method from an event handler

    Hi,
       I'm trying to call a static method of class I designed.  But I don't know how to do it.  This method will be called from an event handler of a web dynpro for Abap application.
    Can somebody help me?
    Thx in advance.
    Hamza.

    To clearly specify the problem.
    I have a big part code that I use many times in my applications. So I decided to put it in a static method to reuse the code.  but my method calls functions module of HR module.  but just after the declaration ( at the first line of the call function) it thows an exception.  So I can't call my method.

  • Breadcrumb in BSP is not working when application is called inside Portal.

    Dear All,
                We have implemented SAP Learning Solution by integrating it with SAP EP 7.0.
    We called the BSP Application HCM_LEARNING inside portal  using BSP iView template.
    The problem we are facing is, the breadcrumbs in the BSP application "HCM_LEARNING"  is working fine when it is called as a standalone application. But the same is not working when it is called inside portal framework page.
    Enterprise Portal breadcrumbs are working fine.
    How could we resolve this.?
    Regards,
    Eben Joyson.

    this is the radio button
    <af:selectOneRadio value="#{bindings.Gender1.inputValue}"
                                                     label="#{bindings.Gender1.label}"
                                                     required="#{bindings.Gender1.hints.mandatory}"
                                                     shortDesc="#{bindings.Gender1.hints.tooltip}"
                                                     id="sor5">
                <f:selectItems value="#{bindings.Gender1.items}" id="si7"/>
              </af:selectOneRadio>
    i use this sample
    http://download.oracle.com/otn_hosted_doc/jdeveloper/11gdemos/ADF_Insider_Essentials/ADF_Insider_Essential_YesNoRadio/ADF_Insider_Essential_YesNoRadio.html

  • Can we call a static method without mentioning the class name

    public class Stuff {
         public static final int MY_CONSTANT = 5;
         public static int doStuff(int x){ return (x++)*x;}
    import xcom.Stuff.*;
    import java.lang.System.out;
    class User {
       public static void main(String[] args){
       new User().go();
       void go(){out.println(doStuff(MY_CONSTANT));}
    }Will the above code compile?
    can be call a static method without mentioning the class name?

    Yes, why do it simply?
    pksingh79 wrote:
    call a static method without mentioning the class name?For a given value of   "without mentioning the class name".
        public static Object invokeStaticMethod(String className, String methodName, Object[] args) throws Exception {
            Class<?>[] types = new Class<?>[args.length];
            for(int i=0;i<args.length;++i) types[i] = args==null?Object.class:args[i].getClass();
    return Class.forName(className).getDeclaredMethod(methodName,types).invoke(null,args);

  • Call a static method via RFC

    Hello people,
    How can I call a static method via RFC?
    I am in SRM and would like to call a static method defined in ECC.
    What is necessary to be configured in this class/method?
    Thanks!!!

    You may need to write RFC in ECC and call it from SRM
    and write following code in RFC for call a static method
    reference_obj=>method_name
    EXPORTING

  • Siri does not work when used for calling a contact function

    Siri does not work when used for calling a contact function

    Very strange, try to turn off/on your iPhone. Or make a reset (home+power button)...
    If it continues try to restore it.

  • Iphone 4 the mike does not work when making a call.  it does work with speaker phone and face time

    Just activated new Iphone 4 and the mike does not work when making a call.  it does work with speaker phone and face time. Any known fixes?

    My wife's iphone 4 also has your same no ring problem as does my iphone 5 just less often.  I called her iphone 4 at home from where I work 20 miles away on my iphone 5.  With her phone in 3G mode ONE call went through and 9 went to voice mail in 10 tries.  This is a 10% sucess rate!!  I also could listen in real time from the wired work phone and the wired home phone.  She turned off 3G mode and it went to "O" mode at  the top left of the screen after the ATT.  In this mode it rang on the first ring tone 5 times out of 5 tries which is 100% of the time.  I have learned that the calling party will hear up to 4 ring tones when placing a call on the ATT network.  If the ring tone changes volume slightly before the 4th ring then and only then will the cell phone start to ring.  If the ring tone does not change after the 4th ring you get voice mail.  If the calling party does not leave a voice message there is never a missed call displayed on the iphone.  If the calling party does leave a voice message a few minutes later the iphone dings for arriving voice mail but still no missed call indication. 
    This appears to be related to the time of day and the ATT tower that the receiving caller is using.  Tried other towers and it works in 3G mode 10 times out of 10 tries.  Apple replaced the first iphone 4 with a new phone, no improvement.  ATT replaced the SIM card and no improvement.  ATT so far is clue less.  This has been going on every since we got the new iphones which is 4 months.  The ATT web site also describes this same problem from several years ago.  Has anyone got better information?
    Thanks

  • Since upgrading to 4.3.3 my iphone 3gs does not ring when receiving a call and the keyboard clicks have gone.

    I have upgraded to 4.3.3 and now the phone does not ring when receiving a call and also the keyboard clicks have disappeared.  Does anyone know the solution to this problem please.?  Thanks

    Many thanks - switch is now on and it now operates corrrectly.

  • Why not to use static methods - with example

    Hi Everyone,
    I'd like to continue the below thread about "why not to use static methods"
    Why not to use static methods
    with a concrete example.
    In my small application I need to be able to send keystrokes. (java.awt.Robot class is used for this)
    I created the following class for these "operations" with static methods:
    public class KeyboardInput {
         private static Robot r;
         static {
              try {
                   r = new Robot();
              } catch (AWTException e) {
                   throw new RuntimeException(e + "Robot couldn't be initialized.");
         public static void wait(int millis){
              r.delay(millis);
         public static void copy() {
              r.keyPress(KeyEvent.VK_CONTROL);
              r.keyPress(KeyEvent.VK_C);
              r.keyRelease(KeyEvent.VK_C);
              r.keyRelease(KeyEvent.VK_CONTROL);
         public static void altTab() {
              r.keyPress(KeyEvent.VK_ALT);
              r.keyPress(KeyEvent.VK_TAB);
              r.keyRelease(KeyEvent.VK_TAB);
              r.keyRelease(KeyEvent.VK_ALT);
                   // more methods like  paste(), tab(), shiftTab(), rightArrow()
    }Do you thinks it is a good solution? How could it be improved? I've seen something about Singleton vs. static methods somewhere. Would it be better to use Singleton?
    Thanks for any comments in advance,
    lemonboston

    maheshguruswamy wrote:
    lemonboston wrote:
    maheshguruswamy wrote:
    I think a singleton might be a better approach for you. Just kill the public constructor and provide a getInstance method to provide lazy initialization.Thanks maheshguruswamy for advising on the steps to create a singleton from this class.
    Could you maybe advise also about why do you say that it would be better to use singleton? What's behind it? Thanks!In short, it seems to me that a single instance of your class will be able to coordinate actions across your entire application. So a singleton should be enough.But that doesn't answer why he should prefer a singleton instead over a bunch of static methods. Functionally the two are almost identical. In both cases there's only one "thing" on which to call methods--either a single instance of the class, or the class itself.
    To answer the question, the main reason to use a Singleton over a classful of static methods is the same reason the drives a lot of non-static vs. static decisions: Polymorphism.
    If you use a Singleton (and and interface), you can do something like this:
    KeyboardInput kbi = get_some_instance_of_some_class_that_implements_KeyboardInput_somehow_maybe_from_a_factory();And then whatever is calling KBI's public methods only has to know that it has an implementor of that interface, without caring which concrete class it is, and you can substitute whatever implementation is appropriate in a given context. If you don't need to do that, then the static method approach is probably sufficient.
    There are other reasons that may suggest a Singleton--serialization, persistence, use as a JavaBean pop to mind--but they're less common and less compelling in my experience.
    And finally, if this thing maintains any state between method calls, although you can handle that with static member variables, it's more in keeping with the OO paradigm to make them non-static fields of an instance of that class.

  • Why there should not be many static methods.

    Why a java class should not have many static methods.How is memory managed in case of static methods.
    If a Utility class has static methods then we can use methods directly by class name but if we do not make the methods static then we can acess methods using object.How does it make the difference.I know that there remains a single copy of class variables/methods.But i want to know how class memory is managed and garbage collected.

    shobhit123 wrote:
    Why a java class should not have many static methods.How is memory managed in case of static methods.
    If a Utility class has static methods then we can use methods directly by class name but if we do not make the methods static then we can acess methods using object.How does it make the difference.I know that there remains a single copy of class variables/methods.But i want to know how class memory is managed and garbage collected.The reason to discourage static methods is stylistic rather than technical. It encourages object oriented thinking.
    Each loaded class is represented by a Class object, and associated with that it a block of memory for static fields. As with all such storage this block is, as viewed from code, rather like an array with one, or in the case of double or long values, two slots per field.
    Because the class loader maintains a map from FQN to Class this storage is only released if the class loader that loaded it becomes unreachable, which happens only in situations like the un-deployment of a web application or EJB.
    Local variables for static methods are allocated in a stack frame, just as with instance methods. There's really no significant difference there.

  • File not found when trying to call a dll on LabVIEW Real Time machine

    I have a dll called "DLLRTTEST" that I've written, and have succesfully called on my host machine.  I'm now attempting to call this dll from a vi that is located on my real time computer.  Currently I get an "Error 7 occurred at Call Library Function Node in DLLRTTEST.vi." message upon execution
    In the attached screenshot I'm trying to ensure that the vi I'm running is in fact located on the real time system.  I then use a "Check if File or Folder Exists.vi" to confirm that the dll that I'm about to call does exist on the real time system as well.  However, I still receive an "error 7 file not found" error from the Call Library Function Node.
    Any help is appreciated.
    Solved!
    Go to Solution.
    Attachments:
    DLL_Call_Screenshot.png ‏61 KB
    DLL_Call_Screenshot.png ‏61 KB

    As nathand already mentioned, depending on your C toolchain your DLL will depend on other DLLs. Usually the according msvcrtXX.dll that matches your Visual C version, if you use Visual C, other runtime DLLs if you use a different C environment. These runtime DLLs are even necessary if you do not call anything in any of your functions, since the DLL makes various initialization steps when it gets loaded and that references some C runtime functions nevertheless. Compiling and linking the DLL with static C runtime is usually also not a clean solution since the linked in C runtime will then reference Windows APIs that are not available on LabVIEW RT.
    Depending on your version of LabVIEW RT you will have some mscvrtxx.dll files in your system directory but it will be an older one than from the latest Visual Studio version. If you can compile your DLL with that Visual Studio version then you should be fine, but could possibly run into new problems if you later upgrade to a newer LabVIEW RT version. Installing the C runtime distributables from newer Visual Studio versions is unfortunately also not a solution, since it references many (undocumented) Windows API functions that are not available in LabVIEW RT.
    Rolf Kalbermatter
    CIT Engineering Netherlands
    a division of Test & Measurement Solutions

  • Calling a static method from another class

    public class Command
        public static void sortCommands(String command, String order, List object)
             if(command.equalsIgnoreCase("merge") && order == "a")
                  object.setAscending(true);
                  Mergesort.mergesort(object.list);                  // line 85
    }and
    public class Mergesort
         public static void mergesort (int[] a)
              mergesort(a, 0, a.length-1);
         private static void mergesort (int[] a, int l, int r)
         private static void merge (int[] a, int l, int m, int r)
    }Error:
    Command.java:85: cannot find symbol
    symbol : variable Mergesort
    location: class Command
    Mergesort.mergesort(object.list)
    What am I doing wrong here? Within the Command class I am calling mergesort(), a static method, from a static method. I use the dot operator + object so that the compiler would recognize the 'list' array. I tried using the
    Mergesort.mergesort(object.list);notation in hopes that it would work like this:
    Class.staticMethod(parameters);but either I am mistaken, misunderstood the documentation or both. Mergesort is not a variable. Any help would be appreciated, I have been hitting a brick wall for hours with Java documentation. Thanks all.

    [Javapedia: Classpath|http://wiki.java.net/bin/view/Javapedia/ClassPath]
    [How Classes are Found|http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html]
    [Setting the class path (Windows)|http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html]
    [Setting the class path (Solaris/Linux)|http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html]
    [Understanding the Java ClassLoader|http://www-106.ibm.com/developerworks/edu/j-dw-javaclass-i.html]
    java -cp .;<any other directories or jars> YourClassNameYou get a NoClassDefFoundError message because the JVM (Java Virtual Machine) can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.
    javac -classpath .;<any additional jar files or directories> YourClassName.javaYou get a "cannot resolve symbol" message because the compiler can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

Maybe you are looking for

  • Acrobat not loading in browser

    PDfs loaded in my browser for years but I had to reinstall Adobe Acrobat Pro and now it will not, when I click on a PDF link it takes me to the "Enter name of file to save to" window. I have everything setup properly. Attached is the about:plugins an

  • Missing Fonts in PDF rush print job!

    Question: Have a brochure supplied by client that has missing fonts, I do not have these fonts.  I have to open the brochure in Illustrator to do alteration as no bleed was included in artwork. Illustrator is going to use a substitue font which is of

  • I am unable to download my music to my ipod shuffle

    i am unable to download my music from library onto my ipod shuffle?

  • Can anyone help me with lightroom 5 - download photos from hard drive and develop on a MAC

    Can anyone help me with lightroom 5 - download photos from hard drive and develop on a MAC@ I have not used this forum before and trying to find my way to assistance with developing photos in Lightroom 5 on a Mac. Have imported the photos from the fo

  • Dmvpn or getvpn or DVTI

    Hello actually i have situation as discuss below and I'm confused about design and implement which VPN topology i have to choose DMVPN, GETVPN or DVTI i have 4 branch and 1 main site, branches have 2 connectivity to HQ one via INTERNET an another via