Re: No late binding for static methods

Only the sale class has a showAdvertisement() method.
When the compiler compiles that class it decides at compile time which exact method is called for each static method call (that's the big difference to non-static method calls: the exact method to be called is only decided at runtime in those cases).
There's only one method showAdvertisement() and that is called in both cases in your code. Since that single method always calls the same static announcement() method, you will see the result you posted.

JoachimSauer wrote:
When the compiler compiles that class it decides at compile time which exact method is called for each static method call (that's the big difference to non-static method calls: the exact method to be called is only decided at runtime in those cases).I thought static methods were bound at classload-time. Demo:
public class A {
    public static void f() {
        System.out.println("A.f");
public class B extends A  {
    public static void f() {
        System.out.println("B.f");
public class C  {
    public static void main(String[] args) {
        B.f();
}If you run the above code, "A.f" is printed, as you would expect -- there is only one static method f defined.
But if you uncomment the code in class B and recompile only it (not C!), then run C, you will get the output "B.f".
If you run javap -c C you will see:
public static void main(java.lang.String[]);
  Code:
   0:   invokestatic    #2; //Method B.f:()V
   3:   returnSo the byte code isn't binding the call to A.f at compile time. The choice is made at classload-time. That is why there are similar restrictions on hiding static methods as for overriding instance methods: [http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.3]

Similar Messages

  • Adobe Form - Data Binding for static text

    Hi Experts,
    I'm trying to create a new label with Adobe Forms (not interactive).
    So I've created an interface with a table LABEL_DATA (import) and many variables (import) and a new Form with Layout.
    I tried to bind some text fields with the imported variables und fields from the table, they're all available in the context, but all fields are still empty (preview and print).
    Field are Text Fields
    Type is : Read Only
    Data Binding is : $record.LABEL_DATA.KDMAT
    Can someone please help me?
    Thank you.
    Regards,
    David

    Hello David,
    In your interface, have you declared LABEL_DATA as GLOBAL Data ?
    You can fill the table and variables in the INITIALISATION in the interface.
    Then you can use the Global table and variables in the form.
    In case you are missing some values; you can debug the code in INITIALISATION section. Get the import tables in INITIALISATION and filter the required values into Global Table and variables.
    Regards
    Sandy

  • Static methods, what for?

    What are static methods for? is there something that is not possible through methods associated with instances (non-static methods) ?

    There are many examples of static mehods in the Java API. For example the Math or the System class. All methods in this classes are static. So you dont have to instantiate the system class. In fact it is even not possible to instantiate the System class. System is final and all Constructors are private.
    Another use for static methods is the access to private static variables.

  • Campaigns with static method

    I have two questions regarding campaign
    1)I have configured campaign for update events of content.
    which will call static method of one class.but when any update occur in repository,exception comes for static method's class not found.
    2)How to avoid stop date in campaign.so that it will run always.
    Can anybody help me on the same

    Since the campaigns run in the enterprise app classloader, anything called by the campaign needs to run in this classloader as well (APP-INF/lib). You could perhaps promote the code you want to call from the campaign from the web-app classloader to the enterprise-app classloader if you want it to be called.
    Code in the enterprise-app classloader (APP-INF/...) can be called by code in the web-app classloader (WEB-INF/...) but not vice-versa. So it shouldn't be an issue to move the code to the ent-app classloader, and it can then be called by both the campaign as well as existing web-app code.
    -Steve

  • Non-static method close() cannot be referenced from a static context

    Friends,
    I am having a little help with some static and not static issues.
    I created a JMenuBar, it's in the file: SlideViewMenu.java
    One of the operations is File->Close and another is File->Exit.
    The listener is in the SlideViewMenu.java file. The listener needs to reference two non-static methods within SlideView.java.
    Here's some of the code:
    SlideViewMenu.java
    public class SlideViewMenu {
        public JMenuBar createMenuBar() {
        final Action openAction = new OpenAction();
        Action aboutAction = new AboutAction();
        ActionListener menuListener = new MenuActionListener();
        JMenuBar menuBar = new JMenuBar();
         // All the menu stuff works fine and is taken care of here.
       // Listener for Menu
       class MenuActionListener implements ActionListener {
         public void actionPerformed (ActionEvent actionEvent) {
              String selection = (String)actionEvent.getActionCommand();
             if (selection.equals("Close"))
              SlideViewFrame.close();
             else  SlideViewFrame.exit();
    }SlideView.java
    // Driver class
    public class SlideView {
         public static void main(String[] args) {
              ExitableJFrame f = new SlideViewFrame("SlideView");
                    // Stuff here, works fine.
    // Frame class
    class SlideViewFrame extends ExitableJFrame {
            // some things here, work fine.
         private SlideViewMenu menuBar = new SlideViewMenu();
         public SlideViewFrame(String title) {
         // Set title, layout, and background color
         super(title);
         setJMenuBar(menuBar.createMenuBar());
            // Stuff here works fine.
         // Handles doing stuff once the file has been selected
         public void setFileName(File fullFileName, String simpleName) {
            // Stuff here works fine.     
         // File->Close. clean up everything.
         public void close() {
              setTitle("SlideView");
              textArea.setText("");
              scrollBar.setVisible(false);
              textArea.setVisible(false);
              statsPanel.setVisible(false);
         // File->Exit.     
         public void exit() {
              System.exit(0);
    }The error I'm getting is:
    SlideViewMenu.java:50: non-static method close() cannot be referenced from a static context
    I don't get it?
    Thanks for all help.

    Making close() and exit() static would not solve the problem because close() requires access to nonstatic member variables/functions.
    Fortunately, that is not necessary. The real reason you are having a problem is that you don't have any way in your listener to access the main frame window, which is what the listener trying to control. You made a stab at gaining access by prefixing the function with the class name, but, as the compiler has informed you, that is only valid for static methods. If you think about it, you should see the sense in that, because, what if you had a number of frames and you executed className.close()? Which one would you close? All of them?
    Fortunately, there is an easy way out that ties the listener to the frame.
    SlideViewMenu.java:public class SlideViewMenu
      // Here's where we keep the link to the parent.
      private SlideViewFrame parentFrame;
      // Here's where we link to the parent.
      public JMenuBar createMenuBar(SlideViewFrame linkParentFrame)
        parentFrame = linkParentFrame;
        final Action openAction = new OpenAction();
        Action aboutAction = new AboutAction();
        ActionListener menuListener = new MenuActionListener();
        JMenuBar menuBar = new JMenuBar();
        // All the menu stuff works fine and is taken care of here.
      // Listener for Menu --- It is assumed that this is a non-static nested
      // class in SlideViewMenu. All SlideViewMenu variables are accessible from
      // here. If this is not the case, simply add a similar member variable
      //  to this class, initialize it with a constructor parameter, and
      // pass the SlideViewMenu parentFrame when the listener is
      // constructed.
      class MenuActionListener implements ActionListener
        public void actionPerformed (ActionEvent actionEvent)
          String selection = (String)actionEvent.getActionCommand();
          // Use parentFrame instead of class name.
          if (selection.equals("Close"))
              parentFrame.close();
            else
              parentFrame.exit();
    }SlideView.java// Driver class
    public class SlideView
      public static void main(String[] args)
        ExitableJFrame f = new SlideViewFrame("SlideView");
        // Stuff here, works fine.
    // Frame class
    class SlideViewFrame extends ExitableJFrame
      // some things here, work fine.
      private SlideViewMenu menuBar = new SlideViewMenu();
      public SlideViewFrame(String title)
        // Set title, layout, and background color
        super(title);
        //Here's where we set up the link.
        setJMenuBar(menuBar.createMenuBar(this));
      // Stuff here works fine.
      // Handles doing stuff once the file has been selected
      public void setFileName(File fullFileName, String simpleName)
        // Stuff here works fine.     
      // File->Close. clean up everything.
      public void close()
        setTitle("SlideView");
        textArea.setText("");
        scrollBar.setVisible(false);
        textArea.setVisible(false);
        statsPanel.setVisible(false);
      // File->Exit.
      public void exit()
        System.exit(0);
    }

  • 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.

  • Implementing interface with static method?

    Hi,
    I need to implement an interface with a static method. I tried to do it both with the interface method static and non-static, but it don't seam to work! Does it work?
    How can I make a work around?
    Thanks!

    Interfaces are designed to provide a contract that a particular object instance guarantees for it's Clients.
    Since a static method does not relate to any particular object instance there is no contract to define...hence you can't have an interface for static methods...
    What you could do however is return an object via a static method that does provide the implementation of the interface...
    i.e.public class MyClass
        static private String myInterfaceImpl = "<some class>";
        static public MyInterface getInterface ()
             return (MyInterface) MyClass.class.forName (MyClass.myInterfaceImpl).newInstance ();
    }That would return an object that provides the interface. I would presume you need the static method so that you don't have to pass around references to a particular object...this method gets around that...you could also create a single object at start up time and return a reference to that eveytime...
    Also, in a way static methods do define an interface...

  • Static method are not overriden ?

    Hi,
    as I try to understand java in depth, I come back with this simple question:
    public class hiddenVar1 {
    boolean aVariable=true;
    public static void main (String [] args) {
    System.out.println("Hello");
    public class hiddenVar2 extends hiddenVar1 {
    boolean aVariable;
    public hiddenVar2() {
    aMethod();
    void aMethod() {
    aVariable = false;
    System.out.println(aVariable);
    System.out.println(super.aVariable);
    public static void main (String [] args) {
    new hiddenVar2();
    hiddenVar1.main(new String[2]);
    The result is :
    false
    true
    Hello
    Conclusion:
    the main static method of hiddenVar1 as not been overrided by the main static method in hiddenVar2.
    So inheritance does not apply for static method ?
    Any explanation ?
    Thank's.
    John

    Firstly i think there should not be two public classes in only one file it gives compile time error.
    Secondly the output is correct.
    Static methods cannot be overriden because it will get initialize when u create instance of the class so u r calling
    -- new hiddenVar2();
    so it calls the constructor in that respective method is called.
    And the second line is calling the super class so it calls the main method of that class and prints "hello"
    hiddenVar1.main(new String[2]);

  • Synchronized on static methods

    It's my understanding that the synchronized keyword attached to a method by default locks on this.
    public synchronized void red () {
      doStuff();
    }is equivelant to:
    public void red () {
      synchronized (this) {
        doStuff();
    }But, what about synchronized on static methods? What is the synchronization object for static methods?
    public synchronized static void blue () {
      doStuff();
    }is equivelant to what?

    oh yeah! man! but you get what I mean, it syncs on theYes, we do, but the compiler doesnt...
    Class object, I guess...Stop guessing...

  • Audio Late Binding Sync

    Hi,
    I want to use audio late binding for a live video stream along with different live audio streams.
    I succeeded to set up the manifest as described here: http://www.adobe.com/devnet/adobe-media-server/articles/late-binding-audio-ams.html
    Everything works fine, but one thing I don't understand is: Upon which information is the audio synchronized with the video at the receiver player?
    Are there some timestamps included in metadata to do this or how is this done?
    I ask, because my task is to manually adjust the delay of different live incoming audio streams to match the video.
    Thanks,
    ueler13

    Hi Scott,
    in simple terms, switching rules are using the f4m declared bitrate to compare to the actual bandwidth. Declaring the same bitrate for all MBR assets will prevent them from working correctly - correct me if I misunderstood what you wrote.
    Switching or changing the alternate audio track will be affected by the buffer size(s). You could expect a maximum lag  equivalent, in the worst case, with the sum of all buffers.
    Regarding the seek and change audio bug, I think it was fixed. http://bugs.adobe.com/jira/browse/FM-1326 is a similar case. You might want to resync with the trunk to get the latest bugfixes, since the sprint 5 was relatively in the middle of the Alternate audio feature development.
    S.

  • Declaring static methods.

    I have some questions about declaring a method as static. If I first write a paragraph about my understanding so far, followed by a code snippet hopefully somebody can help me out.
    It is my understanding (limited) that only one class is loaded by the classloader. When creating an object, all its parameters are stored, but each object does not have its own copy of the methods its class defines, each method. If a method is declared as static then you do not have to create an instance of this class to call this method.
    Code
    /** Get table of codes.
          * @return <code>Hashtable</code> populated with clinical code values.
        public Hashtable getCodeHashtable(HttpServletRequest request) {
            Hashtable htResults = new Hashtable();
            try {
                SpParameter[] spOut = {
                    new SpParameter(OracleTypes.CURSOR, 0)//po_cur_data
                 HOUtils utils = new HOUtils();
                UtilDBquery db = utils.getUtilDBquery(request);
                htResults = db.runGeneralStoredProcedureThrowsExceptions(null, spOut, SP_CodeList, null, true, true);
            } catch (Exception e){
                 e.printStackTrace();
            return htResults;
    Questions
    1) If each object does not carry around its own copy of the methods associated with its class, how does the jvm know which object is calling a method?
    2) In the above code snippet, if I were to make the method static would this have implications if 2 people were to browse to the same page on the app server and run this method at the same time?
    Thanks for any replies.

    1) If each object does not carry around its own copy of the methods
    associated with its class, how does the jvm know which object is
    calling a method?Each object stores one single pointer to its class description. The class
    description contains all the methods (also the static ones). This is a bit
    of a simplification but basically that's all there is to it. When you call
    a method, the address of the method is looked up in that class description
    and it's invoked. For non static methods there's a hidden parameter,
    called 'this' so the method always 'knows' which object it (the method)
    is invoked on. For static methods that hidden parameter simply isn't
    present.
    2) In the above code snippet, if I were to make the method static
    would this have implications if 2 people were to browse to the same
    page on the app server and run this method at the same time?Who knows? This all depends whether or not the method (static or not)
    is reentrant, i.e. does the method have side effects? Does the method
    needs to be synchronized?
    kind regards,
    Jos

  • Support for late binding

    I've created a couple of similar top-level VIs that make use of a number of common subVIs. In the interest of modularity, I'd like to place these common subVIs into some kind of a folder or library that is shared by the top-level VIs - so far, so good.
    Some of these shared subVIs refer to other subVIs that are separately and uniquely implemented for each of the top-level VIs. My original thought was that LabVIEW might use "late binding", when the top-level VI is loaded for execution, to resolve all of the subVI references, and thus pick up the correct uniquely-implemented subVIs for the particular top-level VI being loaded. However, observations have led me to believe that all of the subVI references are resolved statically, at compile time, and thus a shared subVI cannot refer to different subVIs depending on which top-level VI is running. And I can see that it makes sense that it needs to be this way, given the way that the LabVIEW development environment works.
    Consequently, it seems that shared subVIs need to be able to statically resolve any of their own subVI references to one specific implementation at compile time. Thus subVIs that cannot do this cannot be shared subVIs, even though they are otherwise identical in each of the top-level VI contexts. Separate identical copies of such subVIs will need to be maintained for each top-level VI.
    Do I have this approximately correct? Is there any way that this kind of "late binding" can be supported in LabVIEW?
    Solved!
    Go to Solution.

    There are two ways you could make this work; you'll have to determine which one is appropriate for your situation. The first is to use VI reference for the specific instance you want. Inside the generic subVI you use a Call By Reference to call the VI that was passed in. Several of the VIs supplied with LabVIEW demonstrate how this works, for example take a look at "Constrained Non-linear Optimization" under the Mathematics->Optimization palette. You'll see that one of the terminals is are reference to a VI that implements the function to be optimized.
    Another possible solution is to use the dynamic dispatch capabilities of LabVIEW's Object-Oriented Programming. In this case, your library would implement a class. The specific instance you want to call would inherit from that class and define a specific VI that overrides the version in the generic library. When a VI in the library calls the subVI, it will dynamically dispatch to the child method, if the object is of the child class and the child method exists.

  • No static methods for Interfaces ?

    Hi,
    I was going though some documentation about the interfaces and it says that, an interface methods must not be static.
    Can someone please explain or direct me in the direction which tell me why the internet methods cannot be static.
    Thanx.
    Mel

    Defining a static method in a interface would be useless, since static methods can't be executed with dynamic binding. You'd have to know the name of the implementing class of the interface at compile time to call it, which would negate the need for an interface.
    For more details google it, that question has been asked thousands of times.

  • No warning for 'name clash' of static methods

    The background is that I am trying to design a package which allows conversion methods to be 'plugged-in' to a class within a class hierarchy so that users can choose how objects are converted to double values. (For example, a 'city-block' conversion might be used instead of the Euclidean-distance one for class TwoD below.) I am trying to use generics to ensure that only converters specific to the appropriate class can be 'plugged-in' and also to simplify the definition of new converters.
    The issue that has arisen is a case which is not 'type-safe', in that the behaviour is not what I would expect from the type specifications, yet no warning is given. The full code is below.
    public abstract class Base {
        public abstract Converter getConverter();
        public double convert() {
            return this.getConverter().convert(this); // produces a warning
    public class OneD extends Base {
        static Converter<OneD> converter;
        int x;
        public OneD(int x) {
            this.x = x;
        public static void setConverter(Converter<OneD> c) {
            converter = c;
        public Converter getConverter() {
            return converter;
    public class TwoD extends OneD {
        static Converter<TwoD> converter;
        int y;
        public TwoD(int x, int y) {
            super(x);
            this.y = y;
        public static void setConverter(Converter<TwoD> c) {
            converter = c;
        }                                   // compiles OK with no warning
        public Converter getConverter() {
            return converter;
    public abstract class Converter<T> {
        public abstract double convert(T val);
    public class OneDConverter extends Converter<OneD> {
         public double convert(OneD val) {
            return val.x;
    public class TwoDConverter extends Converter<TwoD> {
        public double convert(TwoD val) {
            return Math.sqrt(val.x * val.x + val.y * val.y);
    public class Test {
        public static void main(String[] args) {
            OneD x1d = new OneD(1);
            TwoD x2d = new TwoD(1, 1);
            OneD.setConverter(new OneDConverter());
            TwoD.setConverter(new TwoDConverter());
            System.out.println("Convert OneD(1): " + x1d.convert() +
                               ", Convert TwoD(1, 1): " + x2d.convert());
            TwoD.setConverter(new OneDConverter());
              // wrong type of converter; should not be allowed
            System.out.println("Convert OneD(1): " + x1d.convert() +
                               ", Convert TwoD(1, 1): " + x2d.convert());
    }This produces the output:
    Convert OneD(1): 1.0, Convert TwoD(1, 1): 1.4142135623730951
    Convert OneD(1): 1.0, Convert TwoD(1, 1): 1.0The first line of this shows that the all is working OK, but in the second line we see that the wrong type of converter has been plugged-in to class TwoD, with no complaints or warnings at compile or run-time. I understand why this is so (as a result of erasure), but I am surprised that no warning is given that the static method setConverter() in class TwoD clashes with the one in OneD. Is this really type-safe behaviour? (If the two methods are made instance methods instead of static, the code does not compile because of a 'name clash' error.)
    Incidentally, an 'unchecked warning' arises because of the use of the raw class Converter in class Base, but I cannot figure out how to avoid this. The warning is right, and signals that we cannot be sure that the converter is the appropriate type for the argument. It is up to subclasses to ensure this and I cannot figure out how to guarantee that it will be so.

    public abstract class Base {
    public abstract Converter getConverter();
    public double convert() {
    return this.getConverter().convert(this); // produces a warning
    }Some of the problems you're seeing can be resolved
    by organizing your code better:
    abstract class Base<T extends Base<T>> {
        public abstract Converter<T> getConverter();
        public double convert() {
            return this.getConverter().convert(getThis());
        abstract protected T getThis();
    class OneD extends Base<OneD> {
        static Converter<OneD> converter;
        int x;
        public OneD(int x) {
            this.x = x;
        public static void setConverter(Converter<OneD> c) {
            converter = c;
        public Converter<OneD> getConverter() {
            return converter;
        protected OneD getThis() { return this; }
    class TwoD extends Base<TwoD> {
        static Converter<TwoD> converter;
        int y;
        int x;
        public TwoD(int x, int y) {
            this.x = x;
            this.y = y;
        public static void setConverter(Converter<TwoD> c) {
            converter = c;
        public Converter<TwoD> getConverter() {
            return converter;
        protected TwoD getThis() { return this; }
    abstract class Converter<T> {
        public abstract double convert(T val);
    class OneDConverter extends Converter<OneD> {
         public double convert(OneD val) {
            return val.x;
    class TwoDConverter extends Converter<TwoD> {
        public double convert(TwoD val) {
            return Math.sqrt(val.x * val.x + val.y * val.y);
    class Test {
        public static void main(String[] args) {
            OneD x1d = new OneD(1);
            TwoD x2d = new TwoD(1, 1);
            OneD.setConverter(new OneDConverter());
            TwoD.setConverter(new TwoDConverter());
            System.out.println("Convert OneD(1): " + x1d.convert() +
                               ", Convert TwoD(1, 1): " + x2d.convert());
            TwoD.setConverter(new OneDConverter());  // error
            System.out.println("Convert OneD(1): " + x1d.convert() +
                               ", Convert TwoD(1, 1): " + x2d.convert());
    }

  • 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

Maybe you are looking for

  • Want to make Monday as the first day of the week in GregorianCalendar. how?

    hi I need to know what day is the first of the month is. for example the 1st of Nov 2004 is Moday and 1st of Dec 2004 is saturday. I am using the GregorianCalendar: 1.  GregorianCalendar calendar = new GregorianCalendar(2004,11,1);    //set date to 1

  • Problem with my iPod's right button

    Whenever I push the right arrow button I hear an awful BUMP noise that's annoying, anyone could help remove this ? Or any tips on what could I do ? Thanks.

  • APP - Proposal Run

    Hi Experts, This is relating to error message at proposal run. In F110 after entered payment paratmeters in the second step i am getting payment proposal has been released. After this i am not getting any buttons like edit proposal, payment run are n

  • Purchase Price Variation related issue

    Hi Xperts In GRPO and AP Invoice Quantity and Unit Price is same.  In Invoice CST Included CST is Defined as nondeductible 100%. But in Journal Entry Purchase Price Variation is Affected . Please Clarify Me why this was affected in this account Thank

  • Converting DECIMAL field in R/3 into CHAR field on BW

    Hi all,    We have a field in R/3 which is defined as :       Data Type : Decimal       Length      : 6       Decimal Places : 2      I want to receive it into CHAR field on BW side.      How could I convert it, please. What should be length of this