java - Static class updates static variables, but static methods do not update static variables -


i searched through few pages, seem having comprehension issues static means.the problem have lies in using static class, focuslistener, , actionlistener. class has event handling calls on static class, , when jtextfield fill , tabbed away focuslistener updates static variable instantly. when jtextfields filled , focuslistener has updated variables, there submit jbutton. button clicked, static methods called finish of variables calculated using updated variables. user not aware of this. variables not update though, , curious if implementing wrong? in advance.

import java.awt.*; import javax.swing.*; import java.awt.event.*;  public class wellparameters extends jinternalframe implements focuslistener, actionlistener {     jlabel  measureddepthl, ..., pitgainl, drillbitsizel,                  mudinactivepitsl, mainpanellabel;         jtextfield  measureddeptht, ..., pitgaint, drillbitsizet, mudinactivepitst;         jpanel  mainpanel, firstpanel, secondpanel, thirdpanel, fourthpanel, submitbuttonpanel;         jbutton submitbutton;          wellparameters() {               super("well parameters", true, true, false, true);             this.setbounds(0, 0, 600, 385);             this.setvisible(true);             this.setlayout(new borderlayout());              ...//gui stuff              this.add(submitbuttonpanel, borderlayout.south);         }     @override     public void focusgained(focusevent e) {} //ignore this!      @override     public void focuslost(focusevent e) {         try {             if(e.getsource() == measureddeptht) {                 killwellcalculations.measureddepth = integer.parseint(measureddeptht.gettext());                ...//others               } else if(e.getsource() == mudinactivepitst) {                 killwellcalculations.mudinactivepits = double.parsedouble(mudinactivepitst.gettext());             }         } catch (exception ignore) {}      }     @override     public void actionperformed(actionevent e) {         try {             if(e.getsource() == submitbutton) {             system.out.println(killwellcalculations.pumpefficiency);             killwellcalculations.setpressurebeforecasingburstandformationfracture(); //doesn't work             killwellcalculations.setcirculatingpressures();             killwellcalculations.settriplexpumpcapacity();             system.out.println(killwellcalculations.mudinactivepits);             system.out.println(killwellcalculations.pumpfactor);             system.out.println(killwellcalculations.finalcirculatingpressure);}         }         catch(exception ignore) {}     } } 

that gui, , static class... 2 separate classes. not in same file. package killwellsheet;

public class killwellcalculations {      static int measureddepth;           //total depth open hole bottom          ... //tons of other variables      static double totalstrokes;         //add strokes      //used set different circulating pressures     public static void setcirculatingpressures() {         initialcirculatingpressure = circulatingpressurekillrate + shutindrillpipepressure;         finalcirculatingpressure = circulatingpressurekillrate * (killmudweight/currentmudweight);     }        //calculates capacity of anypipe     private static double pipecapacity(double length, double insidediameter) {         return length * ((insidediameter*insidediameter)/1029.4);     }     //calculates capacity of annulus/open hole     private static double annuluscapacity(double length, double insidediameter, double outsidediameter) {         return length * (((insidediameter*insidediameter)-(outsidediameter*outsidediameter))/1029.4);     }            ... //other functions      //set casing burst pressure     public static void setpressurebeforecasingburstandformationfracture() {         beforecasingburst = burstpressure*0.70;         beforeformationfracture = (0.052*casingshoedepth)*(fracgradientmwequivalent - currentmudweight);     }     public static void bariteneedandvolumeincrease() {         baritesacksrequired = (totalmudvolume/100)*((1099*(killmudweight-currentmudweight))/(28.35-killmudweight));         increaseinmudvolume = 0.091*baritesacksrequired;     }     public static void pumpstrokes() {         surfacetobit = (mudindrillstring)/pumpfactor;         bittosurface = (mudinannulus)/pumpfactor;         totalstrokes = surfacetobit + bittosurface;     } }//end class 

some clarifications... reading code, thing see class wellparameters implements interfaces focuslistener , actionlistener (not static classes). far can figure out code, methods focusgained(focusevent e) , focuslost(focusevent e) implemented former interface , updates values of static class, , depend on same calculated values in actionlistener event actionperformed(). problem facing here race-condition regarding static instance values.

http://java.sun.com/docs/books/performance/1st_edition/html/jpappgc.fm.html a.3.4 unreachable

an object enters unreachable state when no more strong references exist. when object unreachable, candidate collection. note wording: because object candidate collection doesn't mean collected. jvm free delay collection until there immediate need memory being consumed object. it's important note not strong reference hold object in memory. these must references chain garbage collection root. gc roots special class of variable includes

  • temporary variables on stack (of thread)
  • static variables (from class)
  • special references jni native code

  • based on garbage collection documentation, suspect static references of class killwellcalculations eligible garbage collected after call focusevent methods and, reason, not available time event actionperformed() fired.

    you can still use static methods of class killwellcalculations utility class if class used other classes. if not, can transform value class holds calculations you, without static references. need have reference instance of class holds values of calculation... instance:

    public class calculatedvalues {      private int measureddepth;      private double mudinactivepits;       public static calculatedvalues makenew() {           return new calculatedvalues();      }       public void setmeasureddepth(string measureddepthtstring) {            if (measureddepthtstring == null) {                 throw new illegalargumentexception("the measured depth must provided.");            }            try {                 this.measureddepth = integer.parseint(measureddepthtstring);            } catch(numberformatexception nfe) {                 throw new illegalargumentexception("the value provided not interger.");            }      }      public int getmeasureddepth() {           return measureddepth;      }       public void setmudinactivepitst(string mudinactivepitststring) {            if (mudinactivepitststring == null) {                 throw new illegalargumentexception("the measured mudinactivepits must provided.");            }            try {                 this.mudinactivepits = double.parsedouble(measureddepthtstring);            } catch(numberformatexception nfe) {                 throw new illegalargumentexception("the value provided not double.");            }      }      public double getmeasureddepth() {           return mudinactivepits;      }       //...      //...      // more other values/properties important/needed calculation.        public void doallcalculations() {           // have implement logic ones, optionally using same utility/helper static methods            setpressurebeforecasingburstandformationfracture();           setcirculatingpressures();           settriplexpumpcapacity();      } } 

    then, modify constructor of class have instance of value object:

    ...  ... // value object reference values need hold during form interaction private calculatedvalues calculatedvalues;  wellparameters() {       super("well parameters", true, true, false, true);     this.setbounds(0, 0, 600, 385);     this.setvisible(true);     this.setlayout(new borderlayout());      ...//gui stuff      this.add(submitbuttonpanel, borderlayout.south);      // value object reference...     calculatedvalues = calculatedvalues.makenew(); } 

    then, update reference calculation:

    @override public void focuslost(focusevent e) {     try {         if(e.getsource() == measureddeptht) {              //killwellcalculations.measureddepth = integer.parseint(measureddeptht.gettext());             // exceptions thrown can caught in catch below , can display error message value class.             calculatedvalues.setmeasureddepth(measureddeptht.gettext());            ...//others               // collect other values well...          } else if(e.getsource() == mudinactivepitst) {             //killwellcalculations.mudinactivepits = double.parsedouble(mudinactivepitst.gettext());             // try/catch possible runtime exception , display error message             calculatedvalues.setmudinactivepitst(mudinactivepitst.gettext());          }     } catch (exception ignore) {      }  } 

    the updates on final step using instance reference:

      @override     public void actionperformed(actionevent e) {       try {         if(e.getsource() == submitbutton) {         system.out.println(killwellcalculations.pumpefficiency);         // references static method use garbage-collected @ time of call... if should have calculated methods in value class. following:         // killwellcalculations.setpressurebeforecasingburstandformationfracture(); //doesn't work         //killwellcalculations.setcirculatingpressures();         //killwellcalculations.settriplexpumpcapacity();         //system.out.println(killwellcalculations.mudinactivepits);         //system.out.println(killwellcalculations.pumpfactor);         //system.out.println(killwellcalculations.finalcirculatingpressure);}          // final calculation in single method in value object.         calculatedvalues.doallcalculations();          // here getters calculated values of calculations have implement.         system.out.println(calculatedvalues.getmudinactivepits());         system.out.println(calculatedvalues.getpumpfactor());         system.out.println(calculatedvalues.getfinalcirculatingpressure());}          // considering you're done values, clear instance values         // reference not static , not garbage-collected.          calculatedvalues.clearvalues();     }     catch(exception ignore) {}   } 

    i did work way related gui... can see similar example @ http://code.google.com/p/marcellodesales-cs-research/source/browse/trunk/grad-ste-ufpe-brazil/ptf-add-on-dev/src/br/ufpe/cin/stp/ptfaddon/view/swing/execution/jwizardinternalframe.java

    good luck!


    Comments