-3

Consider the following declaration at the top of a class:

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

Can we improve performance on Android with the final keyword?:

static final int intVal = 42;
static final String strVal = "Hello, world!"; 
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
sailakshmi
  • 45
  • 1
  • 2
  • 6
  • 2
    Are you asking or telling? – Basant Singh Nov 04 '14 at 10:16
  • 1
    http://stackoverflow.com/questions/316352/why-would-one-mark-local-variables-and-method-parameters-as-final-in-java The sort answer is neither one. It's just optimization and to prevent logical errors. – Pedro Oliveira Nov 04 '14 at 10:21
  • related discussion around the HotSpot compiler - not specific to Android though - http://stackoverflow.com/questions/4279420/does-use-of-final-keyword-in-java-improve-the-performance – Richard Le Mesurier Nov 04 '14 at 10:27

3 Answers3

3

Using static final fields does improve performance on the Android system.

See the documentation on Performance Tips - Use Static Final For Constants

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.

There are other useful tips on that page too, for performance optimisation. For example there is a 15%-20% increase in performance when accessing static methods vs object methods.


However it is important not to optimise prematurely. Sometimes it is not worth it, and often it can lead to poor coding practices. As they highlight at the bottom of that page:

Before you start optimizing, make sure you have a problem that you need to solve. Make sure you can accurately measure your existing performance, or you won't be able to measure the benefit of the alternatives you try.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
1

No usually... using just final will not improve the performance, Most of the times it use to avoid/achieve following things.

final keyword

  • Which is used to stop method overriding
  • and to stop re assigning the value which is already initialized , means the if the variable is final it cant be initialized
  • A final class is simply a class that can't be extended , means which cannot become a super class

static keyword

  • The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • In other words The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.
  • The static variable gets memory only once in class area at the time of class loading.

static and final together

  • Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared.
  • Declaring them as static final will help you to create a CONSTANT. Only one copy exists which can be accessed anywhere
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
-1

It's good practice to declare constants static final whenever possible as per the performance tips as explained here and below is the clarity between static and final static fields.

static means it belongs to the class not an instance, this means that there is only one copy of that variable/method shared between all instances of a particular Class.For example, consider this

public class MyClass {
public static int myVariable = 0; 
}

//Now in some other code creating two instances of MyClass
//and altering the variable will affect all instances

MyClass instance1 = new MyClass();
MyClass instance2 = new MyClass();

MyClass.myVariable = 5;  //This change is reflected in both instances

final static fields are global constants For eg.

class MyConstants {
   public static final double PI = 3.1415926535897932384626433; 
}
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39