0

I'm using SugarORM to help with SQLite in an Android app. I am using the code below to add new rows to a table:

MyTable d = new MyTable("Row1_Title",valueRow1,stockRow1);
d.save();
MyTable d2 = new MyTable("Row2_Title",valueRow2,stockRow2);
d2.save();
MyTable d3 = new MyTable("Row3_Title",valueRow3,stockRow3);
d3.save();
MyTable d4 = new MyTable("Row4_Title",valueRow4,stockRow4);
d4.save();
MyTable d5 = new MyTable("Row5_Title",valueRow5,stockRow5);
d5.save();
MyTable d6 = new MyTable("Row6_Title",valueRow6,stockRow6);
d6.save();
MyTable d7 = new MyTable("Row7_Title",valueRow7,stockRow7);
d7.save();

MyTable Constructor

public MyTable(String title, int value, int stock){
        this.title = title;
        this.value = value;
        this.stock = stock;
    }

This all works fine, but is this the right way to instantiate a class multiple times? It seems like I should be able to combine the instantiating in some way?

Thanks

1
  • 1
    You could use d = new MyTable() back instead of having all these instances. Commented Jul 22, 2015 at 16:50

2 Answers 2

3

Not really. If you want to initialize different variables with different parameters in the constructor this is the way to do it.

There are other things you can do, but that would involve changing other parts of your code and they aren't any more simple (ex: making an array of MyTable objects).

Sign up to request clarification or add additional context in comments.

Comments

0

You could also make a 2D Object array for the values, which would maybe be a bit more clear, but as @cbender already pointed out, it would change your code somewhere else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.