This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes about variable persistence... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about variable persistence..." Watch "about variable persistence..." New topic
Author

about variable persistence...

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I have a class and I have a variable...

The value of the variable is obtained from the DB.

Now I want to use this value across other java classes.

I have get and set for the variable.As soon as I receive the value from DB, I am setting it in it's set method so that I can call this in other classes...

What should be done in the class that wants to use the same value?

1)create an object of that class
2)if you say object.getVar() ---- it would be null...

should the variable be static? How do achieve it? somebody clarify this concept for me. thanks...
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
If the value of the variable is null can be because

  • the value retrieved from database is null
  • you are not setting the value correctly
  • you are executing first the access to the variable (object.getVar()) and then you are setting the value in that value


  • Which is your flow?

    I don't know if your variable should be static (that depends on how do you want the variable to work) static means that it will be shared by every instance of the class, so imagine you have different users accessing the application, if the first one changes the value of that static then the second one will see the value that the first user has left, this behavour is useful but you have to see if it's what you want. Look at this: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
    Pradeep Adibatla
    Ranch Hand

    Joined: Oct 27, 2009
    Posts: 336
    I have a class;ClassA



    get,set for idVal;

    The value of idVal is obtained from DB and is set in the set method...

    say that the value returned from DB is 3.

    Now I want to use this value whenever I require in other classes...what should be done?

    say I have Classes B,C,D



    similarly if C,D...






    static retains the same copy of the variable across other classes but that's not helping...what could be the changes?
    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    You have to think what you are doing here:

    public ClassA ca=new ClassA();

    you create a new ClassA but that element probably is empty, so you have no values inside, am I wrong?
    Pradeep Adibatla
    Ranch Hand

    Joined: Oct 27, 2009
    Posts: 336
    No,You are right! and I am unable to rectify it and get the value...

    what should be done there?
    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    Yes you are able, of course you are!

    Think what do you want. You get the data from database (and that is a step that as far as I know you have already done) and then you have to set it in the ClassA object that you have created.
    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    Sorry, my answer was not very elaborated, the steps are this:

    data = getDataFromDatabase()
    createClassAObject()
    fillTheClassAObject(data)
    sujith Acharya
    Ranch Hand

    Joined: Dec 25, 2006
    Posts: 60
    to get the value set in the object of type ClassA, in the classes ClassB, ClassC, ClassD, you should have the reference of that object(ClassA) where the value is set , instaed of creating new objects in every class.
    Pradeep Adibatla
    Ranch Hand

    Joined: Oct 27, 2009
    Posts: 336
    @ sujith...

    you mean to say just declare



    in classes B,C,D??

    but the reference would be null isn't it? 'ca' doesn't have any meaning in B,C,D...

    It would be very clear in coding terms...

    I would put that in coding terms...


    @McKenzie ,hope this is what you meant!



    Now say ClassB is accessing it...


    So ClassB would look like...



    Still not clear!
    sujith Acharya
    Ranch Hand

    Joined: Dec 25, 2006
    Posts: 60

    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    No, I was thinking more in something like:



    And you can implement whatever logic you want...
    Pradeep Adibatla
    Ranch Hand

    Joined: Oct 27, 2009
    Posts: 336
    Guys sorry for the delay but I still couldn't solve it!

    This is the exact thing....


    The two classes are DBToVocabularyObjectPersistence and RulesModelAction...

    This the crux of what I was discussing... I have two Arraylists conceptAL and attributeAL as you can observe below in DBToVocabularyObjectPersistence class.. I want the two Arraylists with the populated data in RulesModelAction








    and this is what I am doing in RulesModelAction...





    I want those two ArrayList values from DBToVocabularyObjectPersistence and to set them in this class...
    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    First of all what is the flow of your program? Who calls who and so on? I cannot understand

    Why is this static?



    This:



    Is not correct, use this.vocabproID = vocabproID; (assuming vocabproID is not static of course)

    Why ar you doing this:



    You have four ArrayList, why don't you create an Object with that four elements as attributes and you fill the object, once it's filled you can add it to an ArrayList<myObject>


    How do you get the values to the RulesModelAction class?
    do you really want this: public void getVocabGroupBy() ?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    Please note your code is difficult to read if you don't use th Code button.
    Pradeep Adibatla
    Ranch Hand

    Joined: Oct 27, 2009
    Posts: 336
    First of all what is the flow of your program? Who calls who and so on? I cannot understand



    Actually in the flow, I am in class RulesModelAction...

    Now I need to get the ArrayLists into this class...

    I know these arraylists can be obtained can be obtained from DB...So I am calling this class DBToVocabularyObjectPersistence to fill the ArrayLists and use them in RulesModelAction...


    Why is this static?

    public static String vocabproID;

    This:

    # public void setVocabproID(String vocabproID)
    # {
    #
    # System.out.println("The value passed is ---> "+vocabproID);
    #
    # DBToVocabularyObjectPersistence.vocabproID = vocabproID;
    #
    # System.out.println("The values is :--> "+ DBToVocabularyObjectPersistence.vocabproID);
    #
    #
    # }

    Is not correct, use this.vocabproID = vocabproID; (assuming vocabproID is not static of course)






    This is unused code,removed!!!




    How do you get the values to the RulesModelAction class?
    do you really want this: public void getVocabGroupBy() ?



    That is what my problem,how to get values in RulesModelAction

    That method is fetching values from DB!

    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    Hey Cambpbell, you are right, I changed it, I was sooooo lazy :P

    Pradeep please provide more information about the flow. You only say that you are in the flow but we don't know where are you coming from and which method is calling what.

    I would recommend you to do something like this:

    Create an object that will contain the info from the DB



    then in would your getVocabGroupBy() to something like this:




    Then when you need the information from the database CALL this method and store the result in the ArrayList, then you iterate over it and use the information that contains:




    Do you understand what I mean?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: about variable persistence...
     
    Similar Threads
    Static variables shares the same value across sessions?
    accessing one class variable values in another class
    Accessor (set) / Mutator (get) Methods
    To get DB column names dynamically at run time.
    Problem Using an Array and Object of an Instantiable Class