i’m a new here.
I’m try to do a message list, with the content of Message 1, Message 2 & Message 3.
But code below shows error message.
static final String[] ITEMS = new String[] { "message 1", "message 2", "message 3" };
error message:
Illegal modifier for parameter ITEMS; only final is permitted
however in “ANOTHER PROJECT”, i’m doing a fruit list, using code below.
static final String[] FRUITS = new String[] { "Apple", "Banana", "Coconut" };
and it works perfectly fine. Both codes look exactly the same, so i have no idea where the problems.
below is the complete codes for the message list.
package net.eg.itemlist;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.app.ListActivity;
public class Main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
static final String[] ITEMS = new String[] { "message 1", "message 2", "message 3" };
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.main, R.id.textview_id, ITEMS);
setListAdapter(adapter);
}
}
You cannot use
staticwithin a Method. Move the declaration up to the class-level or remove thestatic.staticmeans it is a value of the class itself rather than of an instance of the class. So if you create 100 instances of your class there is only one shared instance of this variable if you declare itstatic, however there would be 100 instances if you don’t declare itstatic.In Java this kind of variable is only allowed at class-level.
As a side note: In C++ (not sure about C) you could use it inside of methods/functions with a similar semantic: the memory of that variable will be the same each time you call the function/method and the initialization will only be done on the first call. But you cannot address the memory from outside the function/method, thus the variable will be “function/method-private”.