Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving static member value

Status
Not open for further replies.

p27br

Programmer
Aug 13, 2001
516
GB
hello

my problem is the following : i have a Sandwich class which can contain a Vector of SandwichFillings. The SandwichFilling price is the same for any instance of this class so I figured it should be a static class member.
Now, in my application, I am adding instances of Sandwich to a Vector, and when I quit the application, the Vector is written to a file.
What I can't figure out is how to save the static class member, because what I am saving to a file is a Vector object. The idea is that if there is a price change for the SandwichFilling, then it is for all instances of this class.
hope I'm clear enough

thanks

paul
 
If it's static, then it doesn't need saving. It is part of the class definition and will be there when the Vector is repopulated from file later. If you've got the class, you've got the static (I'm assuming here that the static is initialised statically too).

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
timw,

i'm sorry but i am a beginner. i'm not sure i understand : here is my class definition :

Code:
package untitled10;
import java.io.*;
public class Appret implements Serializable {
    private int statutAppret;

    [b]private static double prixAppret;[/b]

    private String nom;
    public  final int ACTIF = 0;
    public  final int RETIRE = 1;
    public  final int SUPPRIME = 2;
    public Appret(String desc) {
        this.nom = desc;
        statutAppret = ACTIF;
    }
    public void setStatutAppret(int statut){
        statutAppret = statut;
    }
    public void setNom(String desc){
        nom = desc;
    }
    public String getNom(){
        return nom;
    }
    public int getStatutAppret(){
        return statutAppret;
    }
    public static void setPrixAppret(double unPrix){
        prixAppret = unPrix;
    }
    public static double getPrixAppret(){
        return prixAppret;
    }

}

what should i change for the value to be kept ?

I have a frame - (using AWT in JBuilder2) - which fetches the value of the static member "prixAppret" and displays it in a textbox

Code:
  void this_windowOpened(WindowEvent e) {
        this.txtPrix.setText(String.valueOf  (Appret.getPrixAppret()));
  }
}

but it doesn't work.

here is the method that sets the price :
Code:
  void btnSave_mouseClicked(MouseEvent e) {
    Appret.setPrixAppret(Double.valueOf(this.txtPrix.getText()).doubleValue());
    this.txtPrix.setText(String.valueOf(Appret.getPrixAppret()));
    this.setVisible(false);
    mainFrame.setVisible(true);
  }

  void this_windowOpened(WindowEvent e) {
        this.txtPrix.setText(String.valueOf(Appret.getPrixAppret()));
  }
}
 
sorry about that, it doesn't show a value in the textbox, but it compiles. It does work as long as I stay in the application but when I load it, then the textbox will be blank, as if it couldn't find a value for that variable.
 
I don't believe that static get/set accessors are supported by the object serialization process, so any value in prixAppret will not be persisted.


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
what do you think of this approach :

class ContainsSandwichFillingsAndPrice
{
Vector SandwichFillings
double SandwichFillingsPrice
}

For persistence it will be written to a file.



 
Well, the approach should work okay. I'm not so hot on the choice of class name though. It's not a good idea to refer to things the class contains in the name. If you ever need to refactor your class contents, then the name will look silly.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
ok,i've implemented the class but I get a java.lang.ClassCastException : java.util.Vector on this line of code :
Code:
g = (GestionApprets)flotLecture.readObject();

here is the GestionApprets class

Code:
package untitled10;
import java.io.*;
import java.util.*;
public class GestionApprets implements Serializable{
  private Vector vApprets;
  private double prixApprets;
  public GestionApprets() {
        vApprets = new Vector();
        prixApprets = 0;
  }
  public void setPrixApprets(double unPrix){
      prixApprets = unPrix;
  }
  public double getPrixApprets(){
      return prixApprets;
  }
  public Vector getvApprets(){
      return vApprets;
  }
  public void ajoutAppret(String desc) {
      Appret a;
      a = new Appret(desc);
      vApprets.addElement(a);
    }

  public void retirerAppret(Appret a) {
      a.setStatutAppret(1);
    }

  public void supprimerAppret(Appret a) {
      a.setStatutAppret(2);
      vApprets.removeElement(a);
    }
}

and the main code :

Code:
      public static Fichier fAppret = new Fichier(sfAppret);
      public static GestionApprets apprets = new GestionApprets();
     apprets = loadGestionAppretsFromFile(fAppret);

the code for the loadfromfile function
Code:
       public static GestionApprets loadGestionAppretsFromFile(Fichier f) {
        GestionApprets g = new GestionApprets();

        if (!f.lirePossible()) {
            System.out.println("erreur lecture"
                               + " " + f.proprietes());
            System.exit(0);
        }
        try {
            ObjectInputStream flotLecture =
                    new ObjectInputStream(
                            new FileInputStream(f));
            g = (GestionApprets)flotLecture.readObject();
            flotLecture.close();
        }

        catch (Exception e) {
            System.out.println(" erreur :" + e.toString());
        }

         return g;
    }

any ideas ?
thanks
 
You're not trying to load an object saved before you made the changes, perhaps? You were saving Vector objects before. You need to load the same Class of object that was saved.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
no there is only one place I'm loading the object and that's in the line of code I posted in my last post.

what i'm not sure of is if i should create a new vector object in the constructor of the GestionApprets class

Code:
  public GestionApprets() {
        vApprets = new Vector();
        prixApprets = 0;
  }
 
No, you misunderstood me. I was thinking that you may have been attempting to load in an object which you'd saved with the previous version of your code. Any objects saved then would now be incompatible with the new versions of your classes.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top