Interface with a static method.

I have tried to define an interface which requires a subclass to implement a static function.
interface Testable {
public static TestFrame getTestFrame();
The idea is the the Testable object must be created with the appropriate constructer arguements, defined by the TestFrame. Thus the TestFrame must be created first.
When I have tried this, the compiler complains that static is not allowed here.
Is there a better way to do this?

When I have tried this, the compiler complains that
static is not allowed here.You can not have static methods in an interface - it would make no sense since static methods are not polymorphically overridden.
>
Is there a better way to do this?Since you need to know the class name in order to instantiate your object anyway, can't you just put the method in the class without it being part of the interface? - If the method is only necessary for object instantiation, then it doesn't sound like it belongs in an interface anyway - clients working with the interface type won't want to use it, so you're just cluttering up the interface.

Similar Messages

  • Why is it that the interfaces cannot have static methods?

    why is it that the interfaces cannot have static methods?

    Interfaces contain polymorphic methods. Static methods are not polymorphic, and therefore would not make any sense to be in interfaces.

  • NEED HELP WITH USING STATIC METHOD - PLEASE RESPOND ASAP!

    I am trying to set a value on a class using a static method. I have defined a servlet attribute (let's call it myAttribute) on my webserver. I have a serlvet (let's call it myServlet) that has an init() method. I have modified this init() method to retrieve the attribute value (myAttribute). I need to make this attribute value accessible in another class (let's call it myOtherClass), so my question revolves around not knowing how to set this attribute value on my other class using a static method (let's call it setMyStuff()). I want to be able to make a call to the static method setMyStuff() with the value of my servlet attribute. I dont know enough about static member variables and methods. I need to know what to do in my init() method. I need to know what else I need to do in myServlet and also what all I need in the other class as well. I feel like a lot of my problems revolve around not knowing the proper syntax as well.
    Please reply soon!!! Thanks in advance.

    class a
    private static String aa = "";
    public static setVar (String var)
    aa = var;
    class b
    public void init()
    a.aa = "try";
    public static void main(String b[])
    b myB = new b ();
    b.init();
    hope this help;
    bye _drag                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Help with JSP / Static methods in beans...

    Hi:
    I am creating several javabeans as part of my application. Is it ok, to make all of them static (no global variables are used) and access in JSP's and Servlets as XXBean.method(var1, var 1). Is this a problem ? (vs creating a new XXBean and using it in every page/servlet).
    I have gut feeling that static methods are better and give better memory usage and performance - as oppose in other scenario I am creating a new Bean for every page access - could not verify. and not sure not creating (new Bean) would give any problems.
    Thanks for sharing.

    No, static methods are not what you want. Just set the "scope" on your use-bean tag to "application".
    <jsp:useBean id="xbean" class="xpackage.XXBean" scope="application" />Alternatively, if what you are trying to do is a pure "function library", then create a tag lib, and add the function definitions like this:
    <function>
         <name>someMethod</name>
         <function-class>xpackage.XXBean</function-class>
         <function-signature>
         java.lang.String someMethod( java.lang.String )
         </function-signature>
    </function>There is a fairly concise description of how to do this at
    http://java.boot.by/wcd-guide/ch07s04.html

  • Why global var can be initialized with a static method and not by other static global var declared after its usage

    Take this:
    class test
    static int i=j;
    static int j=10;
    this will give illegal forward reference ....
    but this will compile successfully ..
    class test
    static int i=test1();
    static test1()
    return 20;
    plz assume we have main method in both cases ..
    java would be loading all static members first and would be assigning default values .. and then will be running all the initializers from to bottom ..
    Why second case is a compile success and not first .. as in second also test1 method is declared after its usage ..
    Plz help.
    Thanks
    Abhishek Roshan

    Why second case is a compile success and not first .. as in second also test1 method is declared after its usage ..
    Because the implementors of Java intentionally chose to do it that way.
    There are TWO stages to the process: preparation (which occurs first) and initialization.
    See the Java Language Spec section 12.4.1 'When Initialization Occurs
    The intent is that a class or interface type has a set of initializers that put it in a consistent state, and that this state is the first state that is observed by other classes. The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (§8.3.2.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.
    Note the clause beginning 'may not refer to class variables'. And the authors give the reason for that restriction in the last sentence: detect circular initializations.
    Then if you check that referenced section 8.3.2.3 you will find this
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3
    8.3.2.3. Restrictions on the use of Fields during Initialization
    The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
      The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
      The usage is not on the left hand side of an assignment.
      The usage is via a simple name.
      C is the innermost class or interface enclosing the usage.
    When a method is used (your example 2) no circular initialization can occur because methods are not 'initialized'.

  • Interface with static methods

    I'm writting a wrapper for exception handling.One of the classes uses log4j to log exceptions.I want to write a interface to generalize loggin.Idealy I should have an interface with certain static methods for loging (i.e logError,logDebugMessage,consoleError,consoleMessage,etc) , but Interface dosent allow that and neither do abstract classes can havstatic method declarations.The implementations of these methods will achieve the hiding of the complexity of using a logging package,log levels etc from the user.
    Let me know how best I can work something out for this.
    Thanks in advance

    Define them once (as final) in an abstract class. Then any subclass can call logError etc.
    Kind regards,
      Levi

  • 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 methods in interfaces

    Java cognoscenti,
    Anyone know why I can't declare a method as static in an interface, but I can in an abstract class. Surely, semantically it's the same - defering the implementation of a static method, not an abstract class and an interface. By the way, I'm using JDK 1.2.2 if that makes a difference

    No, it's not the same. You are not "defering the
    implementation of a static method" because static
    methods are never polymorphic.
    Static methods cannot be abstract for this reason, and
    only abstract methods are allowed in interfaces.I didnt't know that. I thought that - because you can override static methods and you can call them onto an actual instance the method binding would be done at run-time, not at compile-time. So I was trying to prove you wrong, but you are correct !
    A short summary of what I've learnt so far:
    - interfaces cannot contain static methods
    - abstract classes cannot contain abstract static methods
    - static methods can be overriden, but they are not polymorphic; this means that the compiler decides which method to call at compile-time.
    /* output
    SuperClass.someMethod()
    SubClass.someMethod()
    SuperClass.someMethod()
    SuperClass.someMethod()
    SubClass.someMethod()
    SubClass.someMethod()
    SuperClass.someMethod()
    public class Test {
      public final static void main(String[] args) {
          // output: SuperClass.someMethod()
          new SuperClass().someMethod();
          // output: SubClass.someMethod()
          new SubClass().someMethod();
          // output: 2x SuperClass.someMethod()
          SuperClass someClass1 = new SubClass();
          someClass1.someMethod();
          showSuper(someClass1);
          // output: 2x SubClass.someMethod()
          SubClass someClass2 = new SubClass();
          someClass2.someMethod();
          showSub(someClass2);
          showSuper(someClass2); // SuperClass.someMethod()
      public final static void showSuper(SuperClass c) {
            c.someMethod();
      public final static void showSub(SubClass c) {
            c.someMethod();
    class SuperClass {
      public static void someMethod() {
        System.out.println("SuperClass.someMethod()");
    class SubClass extends SuperClass {
      public static void someMethod() {
        System.out.println("SubClass.someMethod()");

  • Implement interface with static variable

    Hello all
    I want to create a class that has a static variable. In this class I also want to create a static function to change this static value. So I donot have to create any object when change the static variable.
    My problem is that this class should implement an interface (using facade pattern). As it implements an interface, it cannot have any static function in it.
    How I can overcome this problem. please help
    Many thanks
    shuhu

    My problem is that this class should implement an interface (using facade pattern).
    As it implements an interface, it cannot have any static function in it.Any class (including one that implements an interface) can have static methods.
    Do you mean you want the static method to be part of the interface? In this case you have a problem. Perhaps you should use an abstract class instead of an interface (the class could possibly implement the interface). Or perhaps you should rethink the need to have a static method.

  • What's the differences between a singleton and a class with static methods

    Hi everybody.
    My question is in the subject. Perhaps "differences" is not the good word. The positive and negative points ?
    Imagine you have to write a connection pool, sure you gonna choose a singleton but why ?
    Thank you very much.

    A class is a class. Java doesn't have (and I wish it
    did) a static outer class. Any class can extend
    another class or implement an interface.A Class object cannot extend or implement anything - it is not under programmer control in any way. You can create a class which implements interfaces, but calling static methods is completely unrelated. Interfaces only affect instance methods, not class ones. I think all of your comparison to C++ is actually confusing you more. In Java, there is a real class object at runtime, as opposed to C++.
    YATArchivist makes a good point about being able to
    serialize, altho I've not met that desire in practice.
    Maybe a concrete example would help.
    mattbunch makes another point that I don't understand.
    A class can implement an interface whether it sticks
    its data in statics or in a dobject, so I guess I
    still don't get that one.See my comment above. Static methods are free from all contractual obligations.
    I prefer instance singletons to singleton classes because they are more flexible to change. For instance I created a thread pool singleton which worked by passing the instance around, but later I needed two thread pools so I made a slight modification to the class and poof! no more singleton and 99% of my code compiled cleanly against it.
    When possible, I prefer to hand the instance off from object to object rather than have everything call Singleton.instance() since it makes changes like I mentioned earlier more feasible.

  • Static Methods in Rmi

    I have a static method in my class .
    Now i want to change my class to Distributed class
    So what to do with that static method ...
    I can't take in interface , so how i can call that method
    Sunil Virmani

    The pre condition of all the remote methods is that they should have been declared in the interface implementing RemoteInterface. Now its been discussed in detail in this forum already why we cannot have static methods declared in the interface therefore by nature not being polymorphic u cannot use static methods for the aforesaid purpose.

  • Static Vs Non-static methods

    Hello,
    I wonder what should I use. I have got a class which does a lot of counting.. I can put the methods inside the class or make a class Math3D with the static methods that will count it.
    Which is the better way?

    BigDaddyLoveHandles wrote:
    deepak_1your.com wrote:
    Sorry mate... did not get that.By definition a utility class has all static methods. So as soon as you mention "utility class" you've already made a decision. The real question is "should method X be static or non-static"? My default position is to assume no method should be static, and then wait to be convinced.
    Look at utility class java.lang.Math, for example. It has static methods sin(), cos(), sqrt(), etc... An obvious choice for a utility class, right? Then they introduced class StrictMath in 1.3 with exactly the same static method signatures. That's a code smell that one should have written an interface and implemented it in at least two ways, but it's too late for that because the original methods are static.Good post.

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

  • Drawbacks of static methods?

    What's the better approach for utility methods:
    a) write abstract class with static methods
    b) write class with non-static methods and instantiate this class to call the public methods.
    ?

    What's the better approach for utility methods:Define "utility methods".
    a) write abstract class with static methodsProbably not - surely a final class would be better than an abstract one?
    b) write class with non-static methods and instantiate
    this class to call the public methods.Do you (or might you ever) want to be able to plug in different implementations? If so, then this is the better option. If not, then a final class with all methods static will probably do the job very well.

  • Cannot use user classes in webservices interface of appmodule custom method

    Hi,
    I need to expose an application module's custom method that uses user defined classes as parameters and return types as a web service interface. I have not been able to do that. Below are more details.
    In particular, I have an app module that has a client interface with a custom method that takes and returns a serializable Emp object (user defined classes/objects). I need to expose this app module as a web service through the use of a service interface.
    When i run the "Create Service Interface" wizard, under service custom methods (Create Service Interface Step 2 of 4 wizard), i don't see my custom method under "Available."
    In addition, the JDev message log says: Business Components: Method "yourCustomMethod(Emp):Emp" cannot be exposed via service interface because the return type "com.mypackage.Emp" is not supported.
    From what i understand by reading the docs below, it seems that in an application module's service interface (Webservice) you only can use simple data type or java.util.list or AttributeList (which is a wrapper for any viewRow). In other words, i cannot use my user defined object in the custom method parameters and return types.
    This seems to be an artificial limitation of webservice creation through the use of app module because one can use complex/user defined objects in methods of a java class and expose them through a webservice.
    I would appreciate if of the tooling PMs can make a well thought out comment.
    11.2.3 What You May Need to Know About Method Signatures on the Service Interface
    http://docs.oracle.com/cd/E12839_01/web.1111/b31974/bcextservices.htm#CJAEHFJD
    9.8.4 What You May Need to Know About Method Signatures on the Client Interface
    http://docs.oracle.com/cd/E12839_01/web.1111/b31974/bcservices.htm#CHDFHGII
    Thanks,
    AJ
    ps: I am using Build JDEVADF_11.1.1.5.0_GENERIC_110409.0025.6013.

    Hi,
    question 2 doesn't apply as the client interface is for Java access to public methods. For the Service Interface, the document lists the supported data types you can use in arguments and return values. To expose a custom methods, what about using a POJO that accesses ADF BC (using create root application module) and then expose your POJO as a JAWS service. This will allow you to expose the data queried from ADF BC through your custom classes.
    Frank

Maybe you are looking for

  • Opening a new browser window always puts a flv-file (0bytes of a youtube vid viewed earlier) on desktop. Firefox 18.02

    Opening a new browser window always puts a flv-file (0bytes of a youtube vid viewed earlier) on desktop. Firefox 18.02 viewed the yuotube vid yesterday. deleted all the flv-files on the desktop. same thing today, a new window always puts the same emp

  • Borderlands 2 keeps on crashing

    On my mac book air, borderlands 2 freezes momentarily and then crashes. It is extremly annoying.

  • Health issues with iMac

    Hello, i own an iMac 27" since late 2009, used it almost every day since buyed and pretty satisfied with this machine. But time showed already the dents on it, dust is behind the glass and the hard drive is already scratching his age But it still wor

  • Setting up rman backups - am I missing something?

    I have inherited an oracle 10g database on Windows and I need to set up RMAN backups. I am going through the steps and I think I am still missing something. I am trying to put the database in archivelog mode and it gives me this error: ORA-01126: dat

  • Adobe Business Catalyst Training

    Hi, I have some experience using Adobe Business Catalyst but have a budget to improve the gaps in my knowledge. Mainly I need help developing custom templates along with the e-commerce side. I am struggling to find any courses on the subject in the U