How can I access stored values of a Bean from inside another Class

六眼飞鱼酱① 提交于 2019-11-29 03:35:10

问题


How can I implement this functionality?

ApplicationConstants.phoneContacts.add(
  new ContactNumberBean(nameOfContact,
    contactNumber, contactNumberType));

ApplicationConstants and ContactNumberBean classes

ContactNumberBean :

package com.example.AddressBook;

public class ContactNumberBean 
{
   private String nameOfContact;
   private String contactNumber;  
   private int contactNumberType;

   public String getnameOfContact()
   {
        return nameOfContact;
   }
   public String getcontactNumber()
   {
        return contactNumber;
   }
   public int getcontactNumberType()
   {
        return contactNumberType;
   }
   public ContactNumberBean(String nameOfContact, String contactNumber,int contactNumberType)
   {
       this.nameOfContact=nameOfContact;
       this.contactNumber=contactNumber;
       this.contactNumberType=contactNumberType;

   }
}

ApplicationConstants :

package com.example.AddressBook;

import java.util.ArrayList;

public class ApplicationConstants
{
    //String[] phoneContacts =new String[10];
    //ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();

    /*String s1,s2;
    int i1;

    ContactNumberBean cb =new ContactNumberBean(str1,str2,i2);

    static ArrayList<String> phoneContacts = new ArrayList<String>();
    phoneContacts.add(s1);
    phoneContacts.add(s2);
    phoneContacts.add(i1);*/

how can implemented in this class functionality ................................

**ApplicationConstants.phoneContacts.add(new ContactNumberBean(nameOfContact,
                                        contactNumber, contactNumberType));**


}

please forward some solution thanks in advance ....Narasimha


回答1:


hi to access been values in another class create a common class named as Constants.java in that declare and initialize been object like below:

public class Constants{

    public static Bean userBeen=new Bean();

}

been class:

public class Been {

    private string countryName;

    public void setCountry(String s) {
        this.countryName=s;
    } 

    public String getCountry() {
        return countryName;
    }
}

set values:

public class A{

    String s="India";

    Constants.userBeen.setCountry(s);

    }
}

get values:

public class B{

    String s=Constants.userBeen.getCountry();

    }

}

this will work fine.



来源:https://stackoverflow.com/questions/6355142/how-can-i-access-stored-values-of-a-bean-from-inside-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!