12

I was just studying about final data members and i thought what would be the difference final variables vs static final variables ??


I understand that a field that is both static and final has only one piece of storage, and final variable will have storages associated with every instance.

But even if i declare a variable only final, then it remains the same for all the objects as i need to initialize them in the program itself and not at the run time.


So, basically there is no difference between the two except for the memory related issue ??

4 Answers 4

14

But even if i declare a variable only final, then it remains the same for all the objects as i need to initialize them in the program itself and not at the run time.

No, non-static final members can be initialized in the constructor. They cannot be re-assigned after that.

Sign up to request clarification or add additional context in comments.

7 Comments

Can i initialize the value of a final variable at run time via user input ??
Only if you are calling the constructor after the user-input is taken. It cannot be changed after the constructor is called.
Only if it is part of the constructor. A way around this is to have another class read in the value first, then create your object with the final variable once you know the data.
Sorry to bother you more.. I am new to java as you can see.. is it possible to call a constructor depending on the user input ?? And if yes, can you give me an example !!
@user1262062.. for that, we'll need some code as to how you are trying to do this. Perhaps a separate question?
|
3

The final means you can assign only one time the value to the variable. The final can be use in many scopes and is very useful, in a object property you must (and forced) to set the value on the declaration, or in the constructor or in the initialization block of the object.

And the static is to set the scope of the variable, that means in a class property that value is storage inside the class, and can be access even without an object, when you use static final or final static you must (and forced) to set the value on the declaration or inside a static initialization code of the class.

Example:

public class NewClass {

    static final int sc = 123; //I recommend to use this declaration style.
    final static int scc;
    final int o = 123;
    final int oo;
    final int ooo;

    static {
        scc = 123;
    }

    {
        oo = 123;
    }

    public NewClass() {
        ooo = 123;
    }

    void method(final int p) {
//        p=123; //Error, the value is only assigned at the call of the method.

        final int m = 123;
        final int mm;
        mm = 123;
//        mm = 456; //Error, you can set the value only once.
        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(m + p); //You still can reach the variables.
            }
        }).start();
    }
}

Comments

2

final variables : A variable declared final will be a constant, its value can not be changed, and can be initialized in the constructor.

static final variable : This must be initialized, either during the declaration or in the static initializer block.

Comments

0

Yes -> They look pretty similar but there are some changes between them! and they are :- non-static final variables =

  • This can be declared and initialized at a time and also we can just declared and initialize using constructor
  • Once we initialized we can not override it
  • these variables can be accessed by its "Name" inside the non-static method but inside the static method we can access it using the object Ref only.

static final variables

  • This must be declared and initialized at same time only.
  • Once we initialized we can not override it and if we are not initializing it we will get compilation error
  • this variables can be accessed by it "Name" it self at any place and any where in the project

I hope this was helpful and happy learning!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.