Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visibility of variables in SQLiteOpenHelper android

why on all demos and tutorials in SQLiteOpenHelper class variables are always: public static final

Have a look:

    public static final String ORDER_ID = "order_id";
    public static final String ORDER_LOGIN_NAME = "login_name";
    public static final String ORDER_RESTO_U_NAME = "resto_uniqe_name";

My question is: I am making an application and its database is too big. So, I will have to make atleast 60-70 variables of these kind. Won't it affect application performance? As these are static variables.

Any help will be highly appreciated....

like image 247
pioneerBhawna Avatar asked Mar 21 '14 09:03

pioneerBhawna


2 Answers

final by means that the value of the variable can not be change further means can not be altered or modified

Using public final static String if we want to create a String that belongs to the class (no instance necessary to use it).

1)Any implementations can change value of fields if they are not defined as final. Then they would become a part of the implementation.An interface is a pure specification without any implementation.

2)So it provide a way for the client to interact with the object. If variables were not public, the clients would not have access to them.

So far as an application and its database if it is too big than yours implementation decides it will be an overhead or not. By using static variables with the contrast of final will not effect the performance!! as public static final String ORDER_ID = "order_id"; will be just a column name so should not be a problem!!

like image 129
Jitesh Upadhyay Avatar answered Sep 18 '22 22:09

Jitesh Upadhyay


Well, whether they public or private or package-protected depends on your needs, but final static is a good way of declaring constants per Android guidelines, take a look here for explanation: http://developer.android.com/training/articles/perf-tips.html#UseFinal

Consider the following declaration at the top of a class:

static int intVal = 42;
static String strVal = "Hello, world!";

The compiler generates a class initializer method, called , that is executed when the class is first used. The method stores the value 42 into intVal, and extracts a reference from the classfile string constant table for strVal. When these values are referenced later on, they are accessed with field lookups.

We can improve matters with the "final" keyword:

static final int intVal = 42;
static final String strVal = "Hello, world!";

The class no longer requires a method, because the constants go into static field initializers in the dex file. Code that refers to intVal will use the integer value 42 directly, and accesses to strVal will use a relatively inexpensive "string constant" instruction instead of a field lookup.

like image 35
nikis Avatar answered Sep 19 '22 22:09

nikis