This discussion is archived
4 Replies Latest reply: Dec 9, 2012 9:46 PM by MrBabakishiyev RSS

Final vs Static Vs Constant

978238 Newbie
Currently Being Moderated
What is difference between final ,static and constant keyword...

Friends i need simple definition with example
  • 1. Re: Final vs Static Vs Constant
    gimbal2 Guru
    Currently Being Moderated
    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-Consulting-com Expert
    Currently Being Moderated
    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 Guru
    Currently Being Moderated
    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 Newbie
    Currently Being Moderated
    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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points