• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about variable persistence...

 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 336
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 336
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No,You are right! and I am unable to rectify it and get the value...

    what should be done there?
     
    Albareto McKenzie
    Ranch Hand
    Posts: 312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry, my answer was not very elaborated, the steps are this:

    data = getDataFromDatabase()
    createClassAObject()
    fillTheClassAObject(data)
     
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 336
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @ 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
    Posts: 60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

     
    Albareto McKenzie
    Ranch Hand
    Posts: 312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No, I was thinking more in something like:



    And you can implement whatever logic you want...
     
    Pradeep Adibatla
    Ranch Hand
    Posts: 336
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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() ?
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please note your code is difficult to read if you don't use th Code button.
     
    Pradeep Adibatla
    Ranch Hand
    Posts: 336
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic