My data model is as follow:
public class CustomerObject implements Serializable
{
public Integer pkid;
public String code;
public String name;
public CustomerObject()
{
pkid = new Integer(0);
code = "";
name = "";
}
}
Now I am calling this from another class:
public CustomerObject getCustObj() {
CustomerObject custObj = new CustomerObject();
custObj.pkid = new Integer(1001);
custObj.code = "CUST1001";
return custObj;
}
Now here in getCustObj() function I want to pass only pkid and code. I mean I want to remove the variable "name" from the object and then pass. So my passing object will look like:
CustomerObject()
{
pkid = 1000;
code = CUST1001;
}
Please help how I can do this.
Actually I have a data model of 200 variable. I will pass this using webservice. But during pass by webservice I may need only 20 to pass. So I want to reduce the data size.
Use another constructor in class CustomerObject as following.
public class CustomerObject implements Serializable
{
public Integer pkid;
public String code;
public String name;
public CustomerObject()
{
pkid = new Integer(0);
code = "";
name = "";
}
public CustomerObject(int inPkid, String inCode)
{
this.pkid = inPkid;
code = inCode;
}
}
When you call getCustomerObject method from another class use as follows
public CustomerObject getCustObj() {
CustomerObject custObj = new CustomerObject(new Integer(1001),"CUST1001");
}
If you are not setting name in your object, then it is as good as object not having name because name is null. You can't remove name variable from the object.
But if you really want to do so, you can use inheritance. Make one class with all attributes except name and other class extends the first class and adds name attribute to it. So now you can use first class when you don't need the name attribute.
Related
Code A works well, I think Code B can work correctly, but in fact, Code B doesn't work correctly. Why?
Why can't I create an object in the function- private void SetField(Context mContext,MAtt aField,String name) ?
Code A
public class MURLPar {
public MAtt diskcount=new MAtt();
public MAtt diskindex=new MAtt();
public MURLPar(Context mContext){
SetField(mContext,diskcount,"Pardiskcount");
SetField(mContext,diskindex,"Pardiskindex");
}
public class MAtt {
public String name;
public String value;
}
private void SetField(Context mContext,MAtt aField,String name){
int id = mContext.getResources().getIdentifier(name, "string", mContext.getPackageName());
aField.name=mContext.getString(id);
}
}
Code B
public class MURLPar {
public MAtt diskcount;
public MAtt diskindex;
public MURLPar(Context mContext){
SetField(mContext,diskcount,"Pardiskcount");
SetField(mContext,diskindex,"Pardiskindex");
}
public class MAtt {
public String name;
public String value;
}
private void SetField(Context mContext,MAtt aField,String name){
aField=new MAtt(); //Create object
int id = mContext.getResources().getIdentifier(name, "string", mContext.getPackageName());
aField.name=mContext.getString(id);
}
}
Because aField gets new memory address when you use the command aField=new MAtt();
As a result memory address of diskcount and diskindex remain uninitialized.
For more check here: https://stackoverflow.com/a/73021/3923800
What's happening in code B is that the MURLPar constructor passes a reference to diskcount/diskindex to SetField, which within that method has the name aField.
You then reassign aField with a reference to a newly created object, and you then manipulate that object. Note that aField is now referring to a completely separate object, and not whatever it was referring to when you entered SetField.
If you're familiar with C you can think of what you're doing here as something along these lines:
void SetField(MAtt *aField) {
aField = (MAtt*) calloc(1, sizeof(MAtt));
}
MAtt *diskcount;
SetField(diskcount);
And then expecting diskcount to have changed after the call to SetField, which it obviously won't have.
If you want something like an out parameter, you can simulate that by returning a newly created object:
private MAtt SetField(Context mContext, String name){
MAtt aField = new MAtt(); //Create object
int id = mContext.getResources().getIdentifier(name, "string", mContext.getPackageName());
aField.name=mContext.getString(id);
return aField;
}
And then:
diskcount = SetField(mContext, "Pardiskcount");
I am struggling with making this code work. Here is my code. First class:
public class PersonalAccount extends Account{
private String cardNumber;
private String cardType;
public ArrayList<PersonalAccount> personalAccounts;
public int personal;
private PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType){
super(first, last, accountNumber);
this.cardNumber = "";
this.cardType = "";
}
public void addPersonalAccount(PersonalAccount aPersonalAccount){
personalAccounts.add(aPersonalAccount);
}
public void getNumberOfPersonalAccounts(){
personal = personalAccounts.size();
}
public void listAccounts(){
for (PersonalAccount personalaccount : personalAccounts){
System.out.println("Personal Accounts");
System.out.println(personalaccount);
}
}
public void findAccount(){
int index = 0;
boolean found = false;
while(index < personalAccounts.size() && !found){
PersonalAccount personalaccount = personalAccounts.get(index);
if (personalaccount.getaccountNumber().equals(accountNumber)){
found = true;
}else{
index++;
}
}
}
}
When attempting to create an instance of this class in another class, it instead creates an instance of the PersonalAccount object. Is there a way around this issue? I am very new to Java and BlueJ it should be noted.
EDIT: sorry I should clarify. I'm trying to call the methods from this class in another class. But when declaring
PersonalAccount class1 = new PersonalAccount();
I get the error: constructor PersonalAccount in class PersonalAccount cannot be applied to given types.
I am trying to call the method on a button click (where numAcc is the button):
numAcc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
int personal;
personal = class1.getNumberOfPersonalAccounts();
}
});
You dont have a default constructor so you cannot create a PersonalAccount like this:
PersonalAccount class1 = new PersonalAccount();
You have to pass the parameters first, last, accountNumber, cardNumber, cardType. It should be something like this:
PersonalAccount class1 = new PersonalAccount("FirstName", "Last_Name", "123456", "123456789", "Visa");
Read this: http://www.dummies.com/how-to/content/how-to-use-a-constructor-in-java.html
You don't have a zero-argument constructor for PersonalAccount which is why the given statement would fail.
Is that the problem you are having?
The problem is here that the constructor is private:
private PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType)
Two things:
You need to change your constructor such that it is public so that it is accessible:
public PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType)
The next thing is to supply parameters such as first, last, accountNumber etc. However, if you declare: public PersonalAccount(), then you would not need to supply arguments when you instantiate the class.
You should now be able to call the methods of this class!
So I have a List of Actors and I want to get each Actors dynamic class name.
For example here is my Actor list: People, Birds, Cows.
I want to get as result the same: "People, Birds, Cows" but without a name attribute in the Actors class. Is it possible?
Example code (here instead of list I used array) :
public Area map[][];
map[0][0] = new AntHillArea();
String name = map[0][0].getClass().getName(); //this results "Area" instead of AntHillArea
Edit: There was other problems with the code, getClass().getName() works fine. Thanks anyway.
String className = obj.getClass().getSimpleName();
Update:
public class Test {
public static void main(String[] args) {
Area map[][] = new Area[1][1];
map[0][0] = new AntHillArea();
String name = map[0][0].getClass().getSimpleName(); // returns "AntHillArea"
System.out.println(name);
}
}
class Area {
}
class AntHillArea extends Area {
}
Use getSimpleName method. It gives you only class and will remove any package if having.
You can do this:
class Dog
{
//code
public String getName()
{
return Dog.class.getName();
}
//better
#Override
public String toString()
{
return Dog.class.getName();
}
}
And similarly for each class. Or have a global one as mentioned in other answers as:
public static String getClassName(Class<?> clas){
return clas.getName();
}
To use Dog dog = new Dog(); getClassName(dog.class);
Ok so this is what I have
public class Register {
public String propertyID;
public String PPSNumber;
Register(String aPropertyID, String aPPSNumber) {
propertyID = aPropertyID;
PPSNumber = aPPSNumber;
}
public void setPPSNumber(String aPPSNumber) {
PPSNumber = aPPSNumber;
}
public String getPPSNumber() {
return PPSNumber;
}
public String getPropertyID() {
return propertyID;
}
}
Then I have this
public static ArrayList<Register> registers = new ArrayList<Register>();
public static void main(String[] args) {
String userInput1 = "", userInput2 = "", userInput3 = "";
userInput1 = JOptionPane.showInputDialog("Enter your PPSNumber");
userInput2 = JOptionPane.showInputDialog("Enter your propID");
registers.add("number", "id");
}
I don't understand why It wont let me add to the ArrayList. Is there some way of adding class types to ArrayLists?
Try this instead :
registers.add(new Register("number","id"));
EDIT 1:
To answer your question, you can create a separate "register" and the use the getters :
Register aRegister = new Register("number","id");
registers.add(aRegister);
System.out.println(aRegister.getPropertyID()+" "+ aRegister.getPPSNumber());
Your List is of type Register so you need to add object of Register class only.
Nothing wrong in create as many Register objects as required.
You can implement toString() method inside Register class then the below sysout will work given the register variable is initialized with Register object. Check this How to override toString() properly in Java? to know about toString implementation.
System.out.println(register)
I have encountered a weird problem in my app (java).
I have an enum. Something like that
public enum myEnum implement myIntrface{
valueA(1),valueb(2),valuec(3),valued(4)
private int i;
// and then - a constructor
public MyEnum(int number){
i = number;
}
private MyObj obj = new MyObj;
// getter and setter for obj
}
and in another class I have this
MyEnum.valueA.setObj(new Obj(...))
in briefe - I have an enum with a private instance member that has a set and a get.
So far so good -
The only thing that amazes me is that later on I look at the value of the MyEnum.valueA().obj is null.
there is nothing that updates the value to null, I have even gave it a default value in the constructor and I still see it null later.
any suggestions?
Enums should be un-modifiable classes so you shouldn't really be doing this. If your looking to modify the state of a type based object like an enum you should use an final class approach with embedded constants. Below is an example of a class based approach with a modifiable name an a un-modifiable name...
public final class Connection {
public static final Connection EMAIL = new Connection("email");
public static final Connection PHONE = new Connection("phone");
public static final Connection FAX = new Connection("fax");
/**/
private final String unmodifiableName; //<-- it's final
private String modifiableName;
/*
* The constructor is private so no new connections can be created outside.
*/
private Connection(String name) {
this.unmodifiableName = name;
}
public String getUnmodifiableName() {
return unmodifiableName;
}
public String getModifiableName() {
return modifiableName;
}
public void setModifiableName(String modifiableName) {
this.modifiableName = modifiableName;
}
}
The purpose of enums is to represent constant values. It does not make any sense to set the fields of a constant value.
You should declare your fields as final, and use the constructor to initialize all of them.
For reference, the following code works as expected:
public class Test {
public static enum MyEnum {
valueA(1),valueb(2),valuec(3),valued(4);
private int i;
private Object o;
private MyEnum(int number) {
i = number;
}
public void set(Object o) {
this.o = o;
}
public Object get() {
return o;
}
}
public static void main(String[] args) {
System.out.println(MyEnum.valueA.get()); // prints "null"
MyEnum.valueA.set(new Integer(42));
System.out.println(MyEnum.valueA.get()); // prints "42"
}
}
the cause of this problem is the db40 framework . It loads an enum from the db using reflection. This is well documented .
http://developer.db4o.com/Forums/tabid/98/aft/5439/Default.aspx