Search code examples
android-sqliteandroid-room

What is the purpose of the line "public Word(@NonNull String word) {this.mWord = word;}" in this example?


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).


Solution

  • 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))