0
int a;
static int a;

Both takes same memory

I just came by this today and i don't know the use of doing so ::

private static final float INDICATOR_RADIUS = 1.0f;

this is same as

private final float INDICATOR_RADIUS = 1.0f;

I'm not getting why they(http://developer.samsung.com/android/samples/Golf) did so?They used this many times

Vineet
  • 111
  • 1
  • 6

5 Answers5

7
int a;
static int a;

Both takes same memory

No, they don't. The first takes up four bytes per instance of your class. The second takes up four bytes, whether there are 0 instances or 100 instances. The field is related to the type, not to any particular instance of the type.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Simply because they want the static variable to have one instance across all objects.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
0

One simple answer is static variables have a single value whole class. in detail, static variables keeps same value for every single object. and also static variables are associate with java class, unlike local variables which are associate with every object of the class

for more detail, follow the Java tutorial

Kirk
  • 4,957
  • 2
  • 32
  • 59
  • I don't like the "for all instances" description, as that suggests that there has to be *one* instance... it's more that the variable isn't associated with any particular instance. – Jon Skeet Jan 07 '14 at 14:27
0

You should start at the basics of JAVA.

A static variable is one that’s associated with a class, not objects of that class. Static variables can be accessed by calling with the class name: ClassName.VariableName.

Ezzored
  • 905
  • 4
  • 10
0

non-static field is unique for every instance and it is know as instance field. But, static fields are Class fields that are shared by all instances of the class. Memory would be same for both fields either it is declared as static or non-static

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53