1

I'm trying to figure out how to use Android's Room library for implementing a prepopulated sqlite database in my app and I came across this Android tutorial. One of the lines (the one in the title) confuses me though, because in another tutorial (also by Android), this line isn't present. Why is this line of code present in the first tutorial but not the second? What is its purpose?

I ask this because my code (which I'm basing off the second tutorial) doesn't include this line and yet this post by a different user attempting to do something similar with a prepopulated database does include it.

Here is some of the code I have (each of the fields has a getter method which just returns this.thatfield'sname):

@Entity (tableName = "words")
public class Words {
    @PrimaryKey
    @NonNull
    @ColumnInfo (name = "word_id")
    private int wordId;

    @ColumnInfo(name = "a_words")
    private String aWords;

    @ColumnInfo(name = "b_words")
    private String bWords;

    @ColumnInfo(name = "c_words")
    private String cWords;

This code gives me a "Cannot find setter for field" but just changing the fields from public to private seems to solve that (not sure if this is the best way to solve this error, though).

noG23
  • 93
  • 2
  • 7

1 Answers1

1

Why is this line of code present in the first tutorial but not the second?

That line is an additional class constructor that takes 1 non-null String and sets the mWord member/variable to the provided String.

Without then you can only use myWord = new Word(); to instantiate a Word object and the value would be either the default value if provided or null.

With the additional constructor then you could use both

 myWord = new Word();

or

 myOtherWord = new Word("A Word");

So, in short it's provided an alternative way of constructing/instantiating a new Object of that Class.

Using your code then you could have, for example :-

@Entity(tableName = "words")
class Words {

   @ColumnInfo(name = "word_id")
   @PrimaryKey
   private int wordId;

   @ColumnInfo(name = "a_words")
   String aWords;

   @ColumnInfo(name = "b_words")
   String bWords;

   @ColumnInfo(name = "c_words")
   String cWords;


   public void setWordId(int wordId, String aWord, String bWords, String c) {
      this.wordId = wordId;
      this.aWords = aWord;
      this.bWords = bWords;
      this.cWords = c;
   }
}
  • Note for demonstration the parameter names use 3 different standards, ideally you would stick to a single standard/convention for naming the parameters.

So now you could use the one constructor that expects 4 parameters e.g.

myWord = new Words(1,"Apple","Banana","Cherry");

which equates to

myWord = new Words();
myWord.wordId = 1;
myWord.aWords = "Apple;
myWord.bWords = "Banana";
myWord.cWords = "Cherry";

As you have specified a constructor, the default constructor is no longer usable.

What is its purpose?

As can be seen, additional constructors, can reduce the amount of coding, there use will also prompt for the values (hence the use of useful parameter names improves i.e. c as above is not very meaningful at all (although in conjunction with the other parameters if would be better than x))

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Just to clarify, the constructor here is just acting as setter in this case, not actually creating a new row with values in the database? You would still have to pass the Words object to a method in the DAO that handles INSERT statements, correct? – noG23 Jun 14 '22 at 20:29
  • 1
    @frothy_Sapling817 Yep it's just setting values when you construct the Object, which you may or may not insert by using an appropriate `@Dao` function. – MikeT Jun 14 '22 at 20:45