0

I am developing a Bluetooth app that is always running until user uninstalls app or switches off their mobile. I am getting an error message: "unfortunately app stopped working". From my initial research this appears to be a memory leak issue. So far I have found out that any object with static reference will leak memory. So I need to remove static references of variables. My project has many constants, how can I write constants without using public static final?

currently, i am using something like this:
public static final int NOTIFICATION_ID=1234;

Update

I think My question needs some more info so here it is

private static variable problem 1) Hi Friends,My app uses database has 9 tables each table has 7 to 13 columns,So the column name I am declaring like private static final String COLUMN_NAME_ACTIVE = "active"; similarly more than 70 static variables has been declared. 2)some of the instances to access in another class i have declared 3)1 util class it is a static class

  • use interface instead defining in class – akhilesh0707 Jul 14 '17 at 13:19
  • 6
    static final constants are not your problem here. They take up some memory, but it's all allocated at startup and never move afterward. You might have a more complex problem. https://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java – litelite Jul 14 '17 at 13:21
  • 3
    "currently i am using like public static final int NOTIFICATION_ID=1234;" -- that is not a memory leak. "When I searched I came to know this is memory leak issue" -- you do not crash from a memory leak, unless you are getting an `OutOfMemoryError`. – CommonsWare Jul 14 '17 at 13:31

5 Answers5

1

You can define constants in res/values under xml format and refer to it using R. In this case R.integer..

Example:

<resources>
    <integer name="notif_id">1234</integer>
</resources>

Example usage:

someMethodName(R.integer.notif_id)

petey
  • 16,914
  • 6
  • 65
  • 97
  • 1
    `R.id`  is just a generated interface full of static final variables. – litelite Jul 14 '17 at 14:06
  • My bad. I didn't realize. I guess this thread might as well be "different ways of making static final variables without using the words 'static' and 'final'" with the number of actually static final answers that are here. – Tomatocurry1 Jul 14 '17 at 14:11
  • That's what it is right now indeed. Because, anyway, static final is the only viable way to store these constants. – litelite Jul 14 '17 at 14:12
0

Create an Interface class and define variables there

public interface Constants{

String Name="AAA";
String Age="20";

}

And use it like this

String name=Constants.Name;
AbhayBohra
  • 2,047
  • 24
  • 36
0

you need to persist data, you can use SharedPreferences like this :

Decalre your SharedPreferences:

SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE); 

write to your SharedPreferences :

Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();

and read from it when you want :

String value = sharedpreferences.getString("key","defaultValue");
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
0

Usually I declare constants in a single file, that will help me and other developers to update any configuration/value.

You can achieve same by using interface. Interfaces are static final by default and variable declarations that are declared to be both static and final.

public interface MyConstants {
  String SERVER_URL = "my_server_address";
  int SPLASH_LAUNCH_TIME = 1000;
}

You can use constants in Interfaces by 2 ways -

a. Either call them directly, like MyConstants.SERVER_URL;

b. Implement that interface in your class and access all constants directly.

Choose your approach wisely, by using approach 2 you will flood all constants in your class. Even they are not required in that class.

Rahul
  • 10,457
  • 4
  • 35
  • 55
  • You should mention that "variables" in interfaces are `static final` *implicitly*, even if not being declares so, which is what the OP wanted to avoid... – Timothy Truckle Jul 14 '17 at 13:37
  • @TimothyTruckle thank you for pointing that, updated my answer – Rahul Jul 14 '17 at 13:41
0

You can use an Enum:

public enum Gender {
   MALE,
   FEMALE
}

And access it like this:

Gender.MALE
Hugo sama
  • 899
  • 1
  • 9
  • 19
  • It seems that in java an enum just generate a class with a bunch of static final inside. https://stackoverflow.com/questions/143285/how-much-memory-do-enums-take – litelite Jul 14 '17 at 15:05