4 Replies Latest reply: Dec 9, 2012 11:46 PM by MrBabakishiyev RSS

    Final vs Static Vs Constant

    978238
      What is difference between final ,static and constant keyword...

      Friends i need simple definition with example
        • 1. Re: Final vs Static Vs Constant
          gimbal2
          There is no constant keyword.

          This is like asking what the difference is between a car and an airplane. If you want to figure out how they work, read the manual. A good Java book is what you need, but you may start with the Java tutorial:

          http://docs.oracle.com/javase/tutorial/
          • 2. Re: Final vs Static Vs Constant
            TPD-Opitz
            http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

            http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html (lookout for "Constants")

            bye
            TPD
            • 3. Re: Final vs Static Vs Constant
              rp0428
              Welcome to the forum!
              >
              What is difference between final ,static and constant keyword...

              Friends i need simple definition with example
              >
              Then you need the Java Language Specification. That is the document that defines the entire Java language including 'final' and 'static'; there is no 'constant' keyword.

              Section 4.14.4 defines final Variables and it includes an example
              http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4
              >
              4.12.4. final Variables
              A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

              It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

              A blank final is a final variable whose declaration lacks an initializer.

              Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.

              This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
              >
              As for static that keyword can be used for: import declarations (7.5.3, 7.5.4), fields in classes (8.3.1.1), static methods (8.4.3.2), member types (8.5.1)

              See those sections for its use.
              • 4. Re: Final vs Static Vs Constant
                MrBabakishiyev
                I suppose that you are C# programmer and interesting with Constant and Final and Static.
                There is not Constant, in Java if you want declare as in C# Constant, you need declare Final Static.

                for example in Java : final static int a = 10;
                in C# : const int a = 10;

                For understand keyword I read it 2 years ago, I advice :
                http://www.amazon.com/Programmers-Guide-Java-SCJP-Certification/dp/0321556054