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.

Similar Messages

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

  • Implement interface (with many variables)

    If i have an interface that has 100 "public static final" variables and I access them by just "implementing" the interface in many other classes (so the variables are accessible via the variable name without any prefix), am I doing something wrong? The other approach of just accessing them thru the <class_name>.<variable_name> is the other option. Any insight, or is this a trivial difference and isn't worth worrying about? Thanks.

    Any insight, or is this a trivial
    difference and isn't worth worrying about? Thanks.Your solution is not considered proper because the class that inherits the constants will also expose them. So what should have been a private business is now a public matter.

  • Runtime error in linking with static variables....

    Hi,
    I am building a shared library which includes a compiled object generated from a class containing the static variables only and also I have another version of same class with same & some new static variables in it and using that to generate another shared library. Now when I tried to run a program
    which links with both the library it core dumps when it tries to return from the program i.e when it finishes.
    Can someone please help me explain why my program is behaving like that?? Can duplicate inculsion of static variables in different scope can cause problem ?? How can this be avoided ?
    Also please be advised that the class with static variables gets generated at the compile time using a script which uses a DTD whose version (specification) can be different in both the libraries therefore I can't just seperate the common stuff into one class and specific into another.
    Thanks.
    Rajeev.

    Not to worry...found the answer in another post. Seems like patches need to applied to the OS.

  • How to import files with static variables into a block with methods?

    i have a problem. is it possible to import files with static variables into tags like
    <%!
    //here i want to import a file named settings.inc
    //whitch is used to set jdbc constants like url,driver,...
    private void method() {
    private void method2() {
    %>
    <%@include file="xy"%>//dosn�t work above
    //only in <%%>tag

    This should be done using either the Properties class or ResourceBundle. Ex.
    <%
    ResourceBundle resBun = ResourceBundle.getBundle(String resource);
    String username = resBun.getString("username");
    String password = resBun.getString("password");
    // etc
    %>

  • 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

  • Polymorphismus with static variables?

    Hi,
    I just have a vatiable which stores some speficic data of the class and one method to return this value. If I declare the code like below getName always return "Superclass" until I override getName with the same body (so it's exactly the same method) in the subclass.
    Cause of this Redunancy, I want to know if there is a way to avoid this?
    Even using return this.name has not the desired effect.
    Thanks a lot in advance.
    Greetings Michael
    Superclass
    private static final String name = "Superclass";
    public  String  getName(){
      return name;
    }Subclass
    private static final String name = "Subclass";

    There is no way Superclass method getName() could access Subclass static member name, unless explicitely.
    In your case, the returned value of Superclass' getName() method is fixed at compilation time.
    As you didn't override the getName() method in Subclass, the Superclass getName() will be invoked for any instance of Superclass or Subclass.
    If you override the getName() method in Subclass, the choice of which method to invoke will be done at runtime.
    To make things easier to understand, always access static members in a static way, i.e. prefix static member access with its class name:class Superclass {
      private static final String name = "Superclass";
      public  String  getName(){
        return Superclass.name;
    }(this code is equivalent to yours)

  • Static variable in cluster

    Hi All,
              How does WebLogic cluster an object, EJB or servlet, with
              static variables? I mean, will the static variables in multiple
              JVMs still have a single value? Or it just can't be clustered?
              Lynch
              

    Statics are only statics per JVM and classloader. This applies to non
              clustering environments as well. If you want a singleton you have to use
              another approach.
              Tinou Bao
              www.tinou.com
              "Lynch" <[email protected]> wrote in message
              news:3b170adb$[email protected]..
              > Hi All,
              >
              > How does WebLogic cluster an object, EJB or servlet, with
              > static variables? I mean, will the static variables in multiple
              > JVMs still have a single value? Or it just can't be clustered?
              >
              > Lynch
              >
              >
              

  • Doubt on static variables.

    Hi,
    I have a class "XYZ" with static variable "x" which is incremented in the constructor of the class. I have two other classes "A" & "B" .
    In class "A" I have created two instances of "XYZ" and printed the value of x. In class B I have created one instance of class "XYZ".
    when I run class A: output is :- x = 1 , x= 2
    When I run class B: output is :- x = 1.
    My question is , In the output of the class B , why isn't the value of x 3? Why is it 1 ? Could anyone please explain.

    Are you sure you aren't running A and B from different processes? I've tried this:public class A {
        public A () {
            new XYZ ();
            new XYZ ();
    public class B {
        public B () {
            new XYZ ();
    public class XYZ {
        private static int c;
        static {
            c = 0;
        public static void printC () {
            System.out.println ("C is " + c);
        public XYZ () {
            c ++;
    public class Test {
        public static void main (String[] parameters) {
            new A ();
            XYZ.printC ();
            new B ();
            XYZ.printC ();
    }and it works fine.
    Kind regards,
      

  • Dealing with a turn based application with using a global static variable

    Hi there. I read some articles about JavaFX concurrency,so as a newbie i have to practice on it. Now i'm trying to implement a turn based application that can be played among 3-6 players. Moreover, i use a scene imported from a .fxml file and i have to update it correctly depends on some calculations of each thread (in other word players ). My main issue is, i don't wanna use a while loop which checks the state of game like;
    while( GameState != State.GAME_OVER ){
         currentPlayer = GameBoard.getNextPlayer();
         // do some actions,calculations etc.
    So, i want to use worker threads instead of this while loop. I guess using Service class for iterating and assigning the next player will be suitable instead of using the while loop and Tasks for doing each player's calculation,or waiting for some responses from Human players on UI based,however, i'm struggling with two problems.
    There should a global static variable ( like GameState which is an Enumarator ) determines the state of the game,so it should be updated and should be checked by each turn. Is there any way to do this ?
    How can i get rid off this while loop ?
    I will appreciate for every answer. Thanks anyway.

    It shouldn't make too much difference. The basic idea is that you have a model class representing your game state (the Game class in jsmith's example). When a player makes a move, you update the state of the game. Since this will result in changes to the UI, this update should be performed on the FX Application Thread.
    If the player making the move is a human player, the move will likely be made by a user action (button press or mouse click, etc); this will be handled on the FX Application thread anyway.
    When the state of the game changes so it is the turn of an "artificial" player to make a move, have the object representing the artificial player calculate its next move, and then update the game state. Since this is a response to the game state changing (it's now the turn of the artificial player), this will also be on the FX Application Thread.
    The only (slight) complexity comes if the calculation of the move for the artificial player takes a long time. You don't want to perform this long running calculation on the FX Application Thread. The cleanest way to manage this is to launch a Task which calculates the desired move, and then updates the game state when the move is ready. So, something like this:
    GameState game = ... ;
    // UI is bound to the game state.
    ExecutorService executorService = ... ;
    final Player currentPlayer = game.getCurrentPlayer() ;
    final Task<Move> calculateMoveTask = new Task<Move>() {
         @Override
         public Move call() {
              Move move = // compute next move...
              return move ;
    calculateMoveTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
         @Override
         public void handle(WorkerStateEvent event) {
              gameState.makeMove(currentPlayer, calculateMoveTask.getValue());
    executorService.submit(calculateMoveTask);
    If you're doing more threading than that, you're probably doing it wrong... . There should also be no need for anything to be held as a "global" static variable (the idea above is the only structural modification you need to the example posted by jsmith).

  • Interfaces: static variables but instance methods, why?

    Why do variables need to be static in interfaces but the methods in them are not allowed to be static? Or, asked differently: Why methods must not be static while variables have to be static in interfaces?
    I don't understand the reason for that difference.

    An interface defines functionality but can not be instantiated as an object. So for the static variables, it can define variables that can be accessed by all implementing classes, but there can only be one definition of them, hence static. Non-static variables fall in the realm of implementation, which is not what interfaces are for.
    Static methods are used to define an implementation that is common among all instances of an object, and must be implemented when defined. Since interfaces can not provide any implementation, static methods are invalid. Remember that static methods are commonly used without instantiating an object (like java.lang.Math methods) so the implementation must be provided when defined.

  • Implementing DOM Interface with existing Java classes

    I had planned on using some tree-like Java classes as a Document Object Model, which would give me access to all sorts of XML and DOM tools like parsers and XSLT transformers. Initially, I thought all that would be neccessary is to implement all the DOM Interfaces in org.w3c.dom and then I would have a set of classes that conformed to DOM Level 1. It was my understanding that interfaces such as DOMImplementation and Document would interface with various XML tools, allowing creation of a class that implements Document and then Document would have its various factory methods that know how to create the various DOM nodes such as Element, Attr, Text, NamedNodeMap, NodeList, etc.
    The problem I'm seeing now is that the JAXP specification (which is what the latest Xerces and Xalan tools conform to) has something called a DocumentBuilder and DocumentBuilderFactory that appear to be necessary to tell the framework what type of class to instantiate that implements the Document DOM interface. Those appear to have a lot of methods that deal with parsing of XML documents and I didn't really want to write or even subclass any existing Parsers in order to get the functionality of traversing and transforming a set of classes that implement the DOM interface.
    Am I missing something here? Is it possible to plug in any (set of classes for) DOMImplementation and get them to work with the various DOM and XML tools out there?
    Is there an easier way to allow parts of an application access to internal data structures but have the more generic tools or APIs, such as XSL transformers, access that same set of classes as a DOM with the generic DOM interface methods?
    Can someone provide me with some guidance here? I'm in the process of finalizing some design on a system and need to know if this is possible or if I need to alter my design.
    Thanks.

    If I understand you correctly, I think I am working on a similar issue. I am unhappy with the methods given by the DOM for retrieving data from the XML file and for building a file. Our software has a bunch of code that uses these classes and it is extremely ugly. My solution was to create a facade on top of the DOM model. Essentially I have some simple classes that store all the pertinent info (for me) about the XML structure. Essentially that is the element or attribute name, its values and in the case of the element, it's children. This makes it easier for me to build and retreive the data. What I then built was a loader class and a builder class. The loader takes an XML file and parses it in using the DOM classes and builds a structure using my classes and returns the root element. The builder takes a root element and creates a DOM object out of it. This frees me of having to code around the DOM classes all over the place and makes it simple to upgrade our XML code if the DOM changes or a better DOM is released. I am using factories to facilitate this and allow me to have loaders for specific types of XML documents so that I can have a class for each which further simplifies the XML related tasks of other developers on my team.

  • Problems with static member variables WAS: Why is the static initializer instantiating my class?!

    Hi,
    I have been hunting down a NullPointerException for half a day to come to
    the following conclusion.
    My constructor calls a method which uses static variables. Since an intance
    of my class is created in the static block when the class is loaded, those
    statics are probably not fully initialized yet and the constructor called
    from the static block has those null pointer problems.
    I've considered moving the initialization of the static variables from the
    declaration to the static block. But your code is inserted BEFORE any other
    code. Therefore not solving my problem.
    Two questions:
    1) what would be a solution to my problem? How can I make sure my static
    variables are initialized before the enhancer generated code in the static
    block calls my constructor? Short of decompiling, changing the code and
    recompiling.
    2) Why is the enhancing code inserted at the beginning of the static block
    and not at the end? The enhancements would be more transparent that way if
    the static variables are initialized in the static block.
    Thanks,
    Eric

    Hi Eric,
    JDO calls the no-args constructor. Your application should regard this constructor as belonging
    primarily to JDO. For example, you would not want to initialize persistent fields to nondefault
    values since that effort is wasted by JDO's later initilization to persistent values. Typically all
    you want to initialize in the no-args constructor are the transactional and unmanaged fields. This
    rule means that you need initialization after construction if your application uses the no-args
    constructor and wants persistent fields initialized. On the other hand, if your application really
    uses constructors with arguments, and you're initializing persistent fields in the no-args
    constructor either unintentionally through field initializers or intentionally as a matter of
    consistency, you will find treating the no-args constructor differently helpful.
    On the other hand, if Kodo puts its static initializer code first as you report, then it is a bug.
    Spec Section 20.16: "The generated static initialization code is placed after any user-defined
    static initialization code."
    David Ezzio
    Eric Borremans wrote:
    >
    Hi,
    I have been hunting down a NullPointerException for half a day to come to
    the following conclusion.
    My constructor calls a method which uses static variables. Since an intance
    of my class is created in the static block when the class is loaded, those
    statics are probably not fully initialized yet and the constructor called
    from the static block has those null pointer problems.
    I've considered moving the initialization of the static variables from the
    declaration to the static block. But your code is inserted BEFORE any other
    code. Therefore not solving my problem.
    Two questions:
    1) what would be a solution to my problem? How can I make sure my static
    variables are initialized before the enhancer generated code in the static
    block calls my constructor? Short of decompiling, changing the code and
    recompiling.
    2) Why is the enhancing code inserted at the beginning of the static block
    and not at the end? The enhancements would be more transparent that way if
    the static variables are initialized in the static block.
    Thanks,
    Eric

  • Why a static class can implements a non-static interface?

    public class Test {
         interface II {
              public void foo();
         static class Impl implements II {
              public void foo() {
                   System.out.println("foo");
    }Why a static class can implements a non-static interface?
    In my mind, static members cann't "use" non-static members.

    Why a static class can implements a
    non-static interface?There's no such thing as a non-static member interface. They are always static even if you don't declare them as such.
    An interface defines, well, a public interface to be implemented. It doesn't matter whether it is implemented by a static nested class or by an inner class (or by any class at all). It wouldn't make sense to enforce that it should be one or the other, since the difference between a static and non-static class is surely an irrelevant detail to the client code of the interface.
    In my mind, static members cann't "use" non-static
    members.
    http://java.sun.com/docs/books/jls/third_edition/html/classes.html#246026
    Member interfaces are always implicitly static. It is permitted but not required for the declaration of a member interface to explicitly list the static modifier.

  • Setting static IP with computer variables with ConfigMgr 2012 R2 + MDT 2013?

    Hi!
    So im trying to set static IP with computer variables in ConfigMgr 2012 R2 with MDT integration. Does not seem to work as expected, after installation it has DHCP enabled.
    This is my config:
    ZTINicConfig.log
    <![LOG[Property ForceCapture is now = ]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[Microsoft Deployment Toolkit version: 6.2.5019.0]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[The task sequencer log is located at X:\WINDOWS\TEMP\SMSTSLog\SMSTS.LOG. For task sequence failures, please consult this log.]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[ZTINicConfig Script Entered.]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[ PHASE = ]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[ Deployment Method = SCCM]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[ Deployment Type = NEWCOMPUTER]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[Capture Network Settings from local machine and write to Environment.]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[Query networking adapters...]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[Networking Adapters found! Count = 1]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[Property OSDAdapterCount is now = 0]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    <![LOG[ZTINicConfig processing completed successfully.]LOG]!><time="14:18:29.000+000" date="03-19-2015" component="ZTINicConfig" context="" type="1" thread="" file="ZTINicConfig">
    And I can't find any of the variables specified in ZTIGather.log. Do they only work with pure MDT or in CS.ini ?
    Any Ideas ?
    Thanks!

    Many methods to achieve the same. I've done something similar once with a small PowerShell script like this:
    http://www.petervanderwoude.nl/post/setting-a-static-ip-address-during-a-deployment-via-powershell-and-configmgr-2012/
    My Blog: http://www.petervanderwoude.nl/
    Follow me on twitter: pvanderwoude

Maybe you are looking for

  • Linking Error

    Hi Gurus, In webi, while linking from detailed report to summary report, am getting an error. Data 10 11 12 33 while linking on 10, it is showing 33 reports which include 10. but the requirement to show is, while clicking on 10, it should display onl

  • MIGO Goods movements issue

    Hello, We are trying to do good movement using BADI  for 101 Movement type for  a Storage location . Is it possible to do regular MIGO along with BADI ? Do we need to activate this sloc as centralized WHS  ? How to control the error message and re tr

  • Business Area in Enterprise Structure

    Can someone please explain the purpose of Business Area/Business Unit in the Enterprise Structure? I'm wondering, functionally, how it is different from personnel area and personnel subarea, as we use the PA and PSA to distinguish  between department

  • My iphone 5 is stuck in recovery mode after trying to update it and know it wont sync to my computer to fix it.... help

    my iphone 5 is stuck in recovery mode after trying to update it and know it wont sync to my computer to fix it.... help

  • Error 30 on new machine

    I have a new MacBook Air and built its disk from a TM backup (sparsebundle) of my previous MacAir - same username etc. I then tried to do a TM save from the new machine. TM asked me if I wanted to create a new sparsebundle or reuse the original (whic