Android - final int field inside onCreate () method

Is it possible to declare the final int field in the Service class and initialize it with some integer that I get from the database query in the onCreate () method?

public class MyService extends Service
{
    private int id;

    @Override
    public void onCreate()
    {
        ... calculate id value ...
        id = <id value derived>;
    }
}

I got this error in Eclipse inside the onCreate () method in the field assignment line:

"The final field cannot be id cannot be assigned"

and this error in the class definition line:

"The blank final field id may not have been inizialized"

As I understand it, this is the correct way to define the field that you want to initialize only once. Can someone help me? Thanks

+5
source share
3 answers

, ; .

onCreate() - , , Activity. onCreate().

+7

, onCreate.

private final int id = 5;

public class MyClass extends Activity {
    private final int id;

    public MyClass() {
        id = 10;
    }
}
+5

id : public final static int id = 5. .

0

All Articles