Static variable is single for class or single for JVM

Hello,
I have doubt, static variable is single for class or single for JVM. as per my knowledge static variable is single for class, but suposse i have an web application (war of an application ) i deployed the application twice with different context, supose yahoo and yahoo1. but the ditrectory structure and classes are same, if i have a statc variable in
a classof an application, if i change the static variable of one class of an application wether it will change the static value of the class of another application.
Example
          yahoo -> web-inf ->      classes ->MainPackage->     MyStaticClass->     static int i=9;
          yahoo1 -> web-inf ->      classes ->MainPackage->     MyStaticClass->     static int i=6;
          if i change the static value i in yahoo application to i=5;
          at the same time if i access the value i in yahoo1 application what i will get (wether 5 or 6).
Thanks.

A static variable is 'single' in its class.
A Class is 'single' in its ClassLoader.
There can be many ClassLoaders per JVM.
There can be many applications per JVM. Each one generally has its own ClassLoader.
Ergo there can be many instances of a Class, and therefore many instances of its static data.

Similar Messages

  • How to access static variable from a Thread class

    Kindly help me.......
    here's the code.....
    class Thread1 extends Thread
    int j=0;
    myClass2 mc = new myClass2();
         public void run()
              for( int a=0;a<6;a++)
                        {try
                             { Thread.sleep(5000);
                             catch(Exception e){System.out.println("Interrupted Exception");}
                             j++;
    mc.change1(i);
    } System.out.println("Thread1 executes "+j+" times");
    class Thread2 extends Thread
    int k=0;
         myClass2 mc1 = new myClass2();
         public void run()
              for( int a=0;a<6;a++)
                        {try
                             { Thread.sleep(5000);
                             catch(Exception e){System.out.println("Interrupted Exception");}
                             k++;
    mc1.change2(i);
    }System.out.println("Thread2 executes "+k+" times");
    class myClass2
    static int i=5;
    public synchronized void change1(int s)
    s=6;
    System.out.println("New value of i:"+s);
    public synchronized void change2(int s)
    s=7;
    System.out.println("New value of i:"+s);
         public static void main(String args[])
    Thread1 b1 = new Thread1();
    Thread2 b2 = new Thread2();
         b1.start();
    b2.start();
    I am unable to pass the variable i in my method call in Thread1: mc.change1(i); and similarly in Thread2:mc.change2(i);

    You can declare your i variable in myClass2 as public static and then simply call there
        mc.change1( myClass2.i ) ;

  • Using static variable in orchestration for each message

    Once a file is dropped to our Biztalk server I am capturing data from each message. However, I need to assign a batchID for this file that I will write to the database along with the data. How do I build my orchestration so that the code I'm using to generate
    the BatchID doesn't create a new ID for each message? I want each message to use the same ID.
    Thanks.
    Raymond

    Shankycheil,
    I think you've pointed out my primary issue. I'm fairly new to Biztalk and I just kind of let it do its thing when it comes to EDI processing. So, I guess my issue is due to the fact that I'm letting the EDI Disassembler debatch the file and pass messages
    into the orch one at a time. I have code in an expression shape that generates a BatchID, but of course if I'm debatching it creates a new BatchID for each message. How do I deal with the file as a whole inside the orchestration so that once I generate a BatchID
    it uses it for all of the messages in file? I've done what Prabhdeep suggested and promoted a field in the schema so that I can set the BatchID, but it needs to be the same for each message in a file.
    Thanks.
    Raymond

  • Why Inner class cannot access static variables

    Why is it that inner class can use only static final variables of the outerclass, and not ordinary static variables of the outer class. "Yes the JLS sepcifies that only final static variables can be used inside an inner class, esp a non blank final variable". But why this restriction.
    Thanks.

    so what are final static variables treated as if they
    are not variables. So if the final static value is
    not loaded when the class is loaded how will the
    class know about the value.??The actual value wil be substituted for the name of a static final value at compile time. That's why you can use them in switch statements where you can't use any variable variable.
    This is something to watch out for, by the way, because if you use a public static final value from one class in another the actual value will be compiled into the using class, so if you change the value where it's defined the class using it will have the old value until it's recompiled.

  • How to read static variable defined java class from flex?

    This is a beginner question. If I use remoteClass to map a java class and a flex class, how can I access a static variable defined in java class from the flex code?
    Thanks!

    Static propeties are by default ignored in the blazeds for serialization. You can try using another global property in the java bean which maps to the value stored in the static property( Hopefully it should work)
    eg,
    public String instanceValue = ClassName.staticValue;
    Ref: http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=serialize_data_3.ht ml

  • Static variables in anonymous classes ..

    How can i access a static variable from an anonymous class ..
    class A{
    outerMeth(){
    Interface anon=new Interface(){
    static int i;
    // how can i acess 'i' ..
    thanx in advance ..

    sorry .. not a valid question ..
    static var are not allowed in anonymous/inner classes ..
    sorry ..
    ignore this one ..

  • Static variable could not seem as attribute in JMX

    Hi,
    I defined a Global class and I have a static variable. I could not see them as attribute in jConsole. BTW, the methods could seem in jconsole. And, I used the class includes static variables as a Global class.
    How can i see the static variable in jconsole?

    Global class is a final Class it is not be instantiated.
    Static variables should be accessible because I want to monitor something ( for example file processing time or file sending time .. ). I set the variable as average time of file processing and I would like to monitor in jmx.
    In application-context xml in spring environment I represent the bean as mbean as below :
         <bean id="exporter3" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
         <property name="beans">
              <map><entry key="bean:name=Global" value-ref="Global"/></map>
         </property>
         </bean>

  • Local static variable not unique between dylib, Xcode 4.4.1 bug?

    I am wondering if this is a Xcode bug or it is the way the c++ standart is.
    I would expect to  see 1 in the console but i see 0.
    Anybody know why? Here is the code
    In one dylib I have
    // Header file
    class Foo {
       int i_ = 0;
       Foo(const Foo&) = delete;
       Foo& operator= (Foo) = delete;
       Foo()
    public:
       void inc()
          ++i_;
       int geti()
          return i_;
       static Foo& get()
          static Foo instance_;
          return instance_;
       Foo( Foo&&) = default;
       Foo& operator= (Foo&&) = default;
    int initialize()
       Foo::get().inc();
       return 10;
    class Bar
       static int b_;
    // cpp file
    #include "ClassLocalStatic.h"
    int Bar::b_ = initialize();
    and in my application project i have
    // main.cpp
    #include <iostream>
    #include "ClassLocalstatic.h"
    int main(int argc, const char * argv[])
       std::cout << Foo::get().geti();
       return 0;
    Thanks

    thanks for your answer, You are right there is 2 foo singleton at different memory addresses.
    I didn't think about when the dylib was loaded. I can't even tell when it is loaded. But I know if i but breaks point I break in the initialize function before breaking in main, but that might be because of this actual setup. So when does a dylib will be loaded? At the first call a program sees that need the dylib?
    Here I'explain why it is done this way. My Singleton is a UnitTestRegistry, the Bar class is a UnitTest class and the static variable b_ is a class containing metadata info on the unit test.
    I have a macro to define the unit test it is called Make_UnitTest()
    So in a cpp I do
    Make_UnitTest(MyTest)
         // Test Stuff
    The macro will get expanded to
    class MyTest_UnitTest: public UnitTestBase
         static UniTestMetaData b_;
         virtual void executeTest();
    int MyTest_UnitTest::b_ = initializeAndRegisterUnitTest("MyTest_UnitTest", factoryFunction);
    MyTest_UnitTest::executeTest()
    // Test Stuff
    And in the function initializeAndRegisterUnitTest I call the getInstance of the unit test  registry. This way my unit test are registred at loading time of the program/dll. If you know a better way to automatically register (i know it cuold be manual but i find it too cumbersome), but it wont fix the problem which is that I have two intance of my singleton because the function is inline in a header.
    Well this behavior is surprising to me, I didn't know it would do this. But hey it is good to learn right!
    Just to make it clear there is no real global!! The singleton is like a global but it is not completly one and the b_ is certainly not global it is a class static memeber...

  • Modifying static variable in 1object dosent effect value in another object

    Hi,
    I have a simple class that declares a static variable x...
    class DeclareStatic
         static int x = 10;
    }I have another class that modifies this static variable (x)...
    class ModifyStatic
         public static void main(String[] Args)
              DeclareStatic.x = 20;
    }The problem I have is when I run the next class to simply print out the static variable x, I get 10 (the originally assigned value), not 20 (the modified value)...
    class StaticTest
         public static void main(String[] Args)
              System.out.println(DeclareStatic.x);
    }It is my understanding that by definition of being static, there is only 1 copy of this static variable(x), shared amongst all objects. Therefore, when I attempt to modify this value with a direct reference (DeclareStatic.x =20;), why isn't the change refelected in other classes which access the variable?
    This also leads to the question how DO I modify a (non final) static variable from an object and have the change reflected in all other objects?
    I have spent some time researching this on-line and in my java books to no avail, any help is greatly appreciated as I am studying to sit the SCJP and attention to detail is everything!
    Thanks,
    Alan Kilbride ;o)

    Hi,
    I have a simple class that declares a static variable
    x...
    class DeclareStatic
         static int x = 10;
    }I have another class that modifies this static
    variable (x)...
    class ModifyStatic
         public static void main(String[] Args)
              DeclareStatic.x = 20;
    }The problem I have is when I run the next class to
    simply print out the static variable x, I get 10 (the
    originally assigned value), not 20 (the modified
    value)...
    class StaticTest
         public static void main(String[] Args)
              System.out.println(DeclareStatic.x);
    }It is my understanding that by definition of being
    static, there is only 1 copy of this static
    variable(x), shared amongst all objects. Therefore,
    when I attempt to modify this value with a direct
    reference (DeclareStatic.x =20;), why isn't the
    change refelected in other classes which access the
    variable?Because your test code never makes a reference to the ModifyStatic class. You could delete ModifyStatic from your system and the code would run exactly the same.
    Jim S.

  • Java inherited static operations on static variables

    How does one force a static method in a base class to operate (read/modify) on static variables in the base class and all of its subclasses ?
    From what I've seen, static methods aren't truly inherited; just invoked from the sub-classes within the super's context !! Am I mistaken ?

    It seems I've incorrectly (more like, incompletely) phrased the 1st part of the question to cause a concern in your response. I hate fundamentally flawed approaches as well :-) My title still holds good, though.
    Rephrasing:
    How does one allow a static method defined in a base class, that can already operate (read/modify) on its own static variables, be inheritable in the subclasses, so that they too can use this method to operate on their over-ridden static variables, without the subclasses having to redefine them ?
    Partial Problem Definition:
    Way before any instances of some of the above-discussed classes are created, the classes should be able to fill in a common static variable with data that is unique to the particular class in question. Especially when these so-called static variables are declared final. Therefore, a superclass (in this case the root) can define this method once, and all subclasses would inherit this class method during their class-object initialization and accordingly fill in their 2 cents worth of value in a given static variable - which in this case will have the same name in all the classes involved. This static variable can represent anything - time-of-class-creation, class-level random number etc.,
    An apparently silly example:
    Class A {
    protected static final String canonicalClassName = getMyClassName();
    protected static String getMyClassName() {
    // Implementation
    Class B {
    protected static final String canonicalClassName = getMyClassName();
    What would the commented implementation in getMyClassName() be, so that all subclasses can take advantage of (WITHOUT re-defining) the super's method ?
    Edited by: Matt0000 on Apr 17, 2008 2:21 PM

  • 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 is static variable.

    What is static variable. what is difference between  static variable and public variable?

    Static variables belong to the Class. Non-static variables belong to Class instances.
    Public variables can be accessed from outside of the Package. Non-public variables can only be accessed within the Package.
    So they are quite different thing. You can have Static variables, Public Static variables, and Private Static variables...

  • Global static variable. I just CANNOT get global for everything

    Hi,
    I copied a connection pool example from oracle web site. It uses static variable. Its not a servlet, its a javabean. I can run a million times in one session and everything is great. If I open up another session, it creates another instance and creates more connections when it should be seeing the previous instance.
    My question is how to make a static variable global to the entire application? Do i have to initialize it in the servlet container? All im doing is calling a JSP page which calls this bean. If instance is null, create 5 new connections. Well like i stated above, it works for a single session. It appears that each session gets its own instance. I have been working and debugging this for a long time and I just cannot come up with a solution...
    Any ideas???
    Thanks as always

    Declar it as static within the servlet class.
    public class MyServlet extends HttpServlet {
    public static ConnectionPool pool;
    Then you can access it from any JSP/Servlet using MyServlet.pool but you may have to import the class into the JSP/Servlet.
    Be aware that there may be synchroniztion issues when you access this static object so you may want to synchronize access to the pool.
      synchronized (application) {
           if (MyServlet.pool == null) { //initialize pool code here }

  • Using a static variable declared in an applet in another class

    Hi guys,
    I created an applet and i want to use one of the static variables declared in teh applet class in another class i have. however i get an error when i try to do that...
    in my Return2 class i try to call the variable infoPanel (declared as a static JPanel in myApplet...myApplet is set up like so:
    public class myApplet extends JApplet implements ActionListener, ListSelectionListener
    here are some of the lines causing a problem in the Return2 class:
    myApplet.infoPanel.removeAll();
    myApplet.infoPanel.add(functionForm2.smgframeold);
    myApplet.infoPanel.validate();
    myApplet.infoPanel.repaint();
    here are some of the errors i get
    dummy/Return2.java [211:1] package myApplet does not exist
    myApplet.infoPanel.removeAll();
    ^
    dummy/Return2.java [212:1] package myApplet does not exist
    myApplet.infoPanel.add(functionForm2.smgframeold);
    ^
    dummy/Return2.java [213:1] package myApplet does not exist
    myApplet.infoPanel.validate();
    ^
    dummy/Return2.java [214:1] package myApplet does not exist
    myApplet.infoPanel.repaint();
    ^
    please help! thanks :)

    I don't declare any packages though....i think it just doesn't recognize myApplet for some reason..
    other errors i got compiling are:
    dummy/Return2.java [82:1] cannot resolve symbol
    symbol : variable myApplet
    location: class Return2
    updateDesc.setString(3, myApplet.staticName);
    I Don't get why i'm getting this error cuase they worked fine when myApplet was a standalone application, not an applet.
    myApplet is in the same folder as Return2 and it compiles properly.

  • How can I write an instance of a class in a static variable

    Hi !
    I have an instance of a class
    Devisen dev = new Devisen();
    In an other class I have a static method and I need there the content of some variables from dev.
    public static void abc()
    { String text=dev.textfield.getText()
    I get the errormessage, that the I cannot use the Not-static variable dev in a static variable.
    I understand that I cannot reference to the class Devisen because Devisen is not static I so I had to reference to an instance. But an instance is the same as a class with static methodes. (I think so)
    Is there a possibility, if I am in a static method, to call the content of a JTextField of an instance of a class ?
    Thank you Wolfgang

    Hallo, here is more code for my problem:
    class Login {
       Devisen dev=new Devisen();
    class Devisen {
       JTextField field2;
       if (!Check.check_field2()) return; // if value not okay than return
    class Check {
       public static void check_field2()
         HOW TO GET THE CONTENT OF field2 HERE ?
    One solution ist to give the instance to the static function, with the keyword "this"
    if (!Check.check_field2(this)) return;and get the instance
    public static void check_field2(Devisen dev)BUT is that a problem for memory to give every method an instance of the class ? I have 50 fields to control and I dont want do give every check_method an instance of Devisen, if this is a problem for performance.
    Or do I only give the place where the existing instance is.
    Hmm...?
    Thank you Wolfgang

Maybe you are looking for