Can anyone explain why this snippet of code is working in the constructor only?

100 views Asked by At

im a beginner in Java as well as Android development, who recently took up the task of developing a simple Android application however i recently faced a problem which i was able to resolve through trial and error. As i cant figure out the exact reason for this behavior, Can i get some insight please.

Theres a class Flags declared as:

public class Flags {

private String mName;
private int mFlag;

Flags(String name, int flag) {
    mName=name;
    mFlag=flag;
}
}

Im using this Flags class object in another class known as Page as

public class Page {

private Flags[] mFlags;
//This code is giving me error
mFlags = new Flags[20];
mFlags[1] = new Flags("America", R.mipmap.America);
mFlags[2] = new Flags("Australia", R.mipmap.AUSTRALIA);




Page()
{
 //This code is correct and not giving any error.
    mFlags = new Flags[20];
mFlags[1] = new Flags("America", R.mipmap.America);
mFlags[2] = new Flags("Australia", R.mipmap.AUSTRALIA);
mFlags[3] = new Flags("Austria", R.mipmap.AUSTRIA);
mFlags[4] = new Flags("Bangladesh", R.mipmap.BANGLADESH);
mFlags[5] = new Flags("Canada", R.mipmap.CANADA);
mFlags[6] = new Flags("Chile", R.mipmap.CHILE);
mFlags[7] = new Flags("China", R.mipmap.CHINA);
}





}

Why am i allowed to create and initialise the Flags class object inside the constructor of my Page class and not outside it? Why is this error resolved by placing my initialisation inside the constructor? Thank you in advance.

2

There are 2 answers

1
Michael P On BEST ANSWER

Every statement needs a scope, or in other words, statements can be run only inside methods. when you write mFlags[1] = new Flags("America", R.mipmap.America); in a class but not in a method, it makes no sense. When is this code supposed to run? on which stack?

when you run this: private final Flags[] mFlags = new Flags[20]; the scope is the constructor. It's equivalent to defining private final Flags[] mFlags and then writing mFlags = new Flags[20]; in the beginning of your constructor. (every constructor you have).

Java just makes it easier for you to define properties and initialize them in the same line for better visibility and to save you from initializing it in every constructor overload.

Same goes for

private final Flags[] mFlags = new Flags[20];

   // initializer block
   {
      mFlags[1] = new Flags("America", R.mipmap.America);
      mFlags[2] = new Flags("Australia", R.mipmap.AUSTRALIA);
      // ...
   }

}

This is a piece of code that is in the constructor scope. To verify this, you can throw and exception in one of this blocks and look at the stack trace.

1
Kevin Krumwiede On

You generally can't have statements outside a method body, unless you're assigning a variable or it's an initializer block.

public class Page {
   private final Flags[] mFlags = new Flags[20];

   // initializer block
   {
      mFlags[1] = new Flags("America", R.mipmap.America);
      mFlags[2] = new Flags("Australia", R.mipmap.AUSTRALIA);
      // ...
   }

}