Results 1 to 8 of 8
  1. #1
    hisheeraz is offline Member
    Join Date
    Apr 2011
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0

    Exclamation final VS static keywords

    hey guys
    im new to java finding it very interesting
    but i have a little confussion
    what is the different between final keyword and static keyword ?

    i am using this statement

    final static int LIGHTSPEED = 300000;

    and i am getting compiler error, compiler is telling me that only final is required
    i understand that any one can please explain me that different please.

    thanks
    -hisheeraz

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    My compiler doesn't give an error and I can't see why yours would. Static means that one copy is made for all instances of that class, while final means the variabele can't be modified. So they are completly different things.

  3. #3
    hisheeraz is offline Member
    Join Date
    Apr 2011
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0

    Question

    Thanks for the reply
    I'm using eclipse just downloaded today
    When I remove static keyword from the statement then it is
    Working fine. Where ad the book that I'm using to study java
    Is using complete statement I.e

    final static int LightSpeed =300000;

    My book is The Java Way by Jerard Sparke
    According to the author there should not be any error
    But my comp won't let me go pass that line.

    Thanks for your time any ways.

    What should I do?

    Regards
    -hisheeraz

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    This is the time you show us a sscce, a short, self-contained, compilable example. Try to reproduce the error in the smallest amount of self-contained (=runnable) code possible and then show us that code. Then we can help you further.

  5. #5
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    I dont know your code but is that variable LIGHTSPEED in a method in your class. Any static variable shouldn't be in a method, it should be outside all methods in your class. when i put a static variable in a method eclipse shows a similar error message. try to put that LIGHTSPEED variable outside all of your methods and see if that works

    correct way
    Java Code:
    public class YourClass {
    
    final static int LIGHTSPEED = 300000;
    
    public void exampleMethod() {
    
    }
    
    }
    wrong way
    Java Code:
    public class YourClass {
    
    public void exampleMethod() {
    
         final static int LIGHTSPEED = 300000;
    }
    
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Roedy Green discusses static and final in his handy Java glossary.

  7. #7
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    When applied to variable, final static keywords make it a constant that is accessible from static context - in plain english, there is no need to create an object of the class where its located to access it. final makes the variable unchangeable at runtime - that is, makes it a constant.

    So here is where it makes sense:
    Java Code:
    public class Universe {
    public final static speedOfLight = 300000000;
    
    }
    Hence, any other object can get that variable as such: Universe.speedOfLight;
    It makes sense to make speedOfLight in the universe a public final static as it is not something that could change at program runtime, and it should be accessed by all objects interacting with the Universe.

    If however, you're coding a spaceship in that universe, it would not make sense to make its speed final, as it could change, or static, as other objects would probably not need to find that information if the specific object for the spaceship doesn't exist yet.

    Look in the
    Java Code:
    Math
    class for good examples on how to use finals and statics.

  8. #8
    hisheeraz is offline Member
    Join Date
    Apr 2011
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0

    Default

    OMG, thank you all
    especially Kyle227 for pointing out to me about the position of the statement
    in my lousy code and also thanks to Maximus-EVG for explaining the difference
    in plain english.
    I was writing my statement inside the main method thats why i was getting that error
    now that i have moved the statement out of the method and placed it in the class
    it is working fine.
    and of course everyone else who made an effort for me
    cheers

Similar Threads

  1. Final variables shouldn't also be static?
    By emilioJazz in forum Advanced Java
    Replies: 5
    Last Post: 11-29-2010, 11:10 AM
  2. static final int DONE = (1 << 9) - 1; ???
    By alacn in forum New To Java
    Replies: 2
    Last Post: 07-26-2010, 05:31 PM
  3. Final,abstract and static
    By $hr!k@nt in forum Advanced Java
    Replies: 5
    Last Post: 01-09-2008, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •