当前位置: 动力学知识库 > 问答 > 编程问答 >

java - When to choose variables to declare as final static

问题描述:

I have used final and static variables as well. what i found about these variables is,

final variable

  • A final variable can only be initialized once, either via an initializer or an assignment statement.

  • Unlike the value of a constant, the value of a final variable is not necessarily known at compile time.

what variables should i declare as final-

Most often i use those variables whose value is constant universally and can never changed, such as the value of PI.

public static final double PI = 3.141592653589793;

static variables

  • These are those variables which belongs to the class and not to object(instance).

  • Static variables are initialized only once , at the start of the execution .

  • A single copy to be shared by all instances of the class

  • A static variable can be accessed directly by the class name and doesn’t need any object.

what variables should i declare as final-

Most of the time, i use those variables which i want to initialize only once and use them in the enitre class.

When to use final static variable

Now, i came across a term final static in one of my database project. I found that some of the database objects or even database version were declared as statci final.

 static final String DATA_BASE = "BackUpDatabase.db";

static final int DATA_BASE_VERSION = 1;

Now, my question is what variables should we declare as final or static or final static, as using either of them could have solved the issue, then wyh to use both 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 as @Vulcan told. Only one copy exists which can be accessed anywhere.

网友答案:

Variables declared as static final (or vice versa) are understood to be meaningful constants, and are named in all upper-case with underscores for spaces.

An example of a commonly encountered constant is Integer.MAX_VALUE, or Math.PI.

网友答案:
static -  Only use when a variable which is used globally 
final -  Only use when you need to declare a value as constant 

static final - Only use when a value is globally used and it is a constant.

: - Here global means across all the instances of a java class
网友答案:

- static in java means Class's member. Its shared by all the instances of the class.

- final keyword in java means, constant, but has different interpretation depending on what its being applied.

- When we use static final on a field, consider it as a Global variable.

- PI is static variable of Math Class and its directly accessed using the class name, as Math.PI.

- Use all letters in caps to define a static final variable.

final's interpretation:

final variable : Its value canNot be changed

final method : It canNot be overridden

final class : It canNot be extended

final Parameter : Its value canNot be changed which it receives from caller's argument

final Object Reference Variable : It canNot refer to any other object, other than the one its currently referring to

网友答案:

final only says that value once initialized can't be changed; static says that the attribute belongs to Class and NOT objects.

So when you say final static; this means there is just one copy of variable and it can't be changed.

网友答案:

Static Variable

  • You can change the static variable value by calling static method present in same class .
  • Static variable value will be same for all object created from this class . if we change the value then all object of that class will get new value ,old value will be lost.
  • Value can be changed multiple times.

Final variable

This variable value can be initialized by two way:

  1. At the time of declaring the variable.
  2. At the time of creating object of that class where class constructor will have this.finalvariable = newfinalvariablevalue;

Once initialized it can't be changed by any method (Static or non-static).

分享给朋友:
您可能感兴趣的文章:
随机阅读: