Results 1 to 13 of 13

Thread: Final int

  1. #1
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default Final int

    Hi, im following a tutorial from a link provided by someone on this forum that creates a red Square to help me learn Graphics2d. But in one method the tutorial uses 'final int' instead of just 'int' with no explaination of why. I know this seems stupid butcould someone explain why please.

    Java Code:
    final int CURR_X = redSquare.getX();
    final int CURR_Y = redSquare.getY();
    final int CURR_W = redSquare.getWidth();
    final int CURR_H = redSquare.getHeight();
    final int OFFSET = 1;
    Thx

    Fireking

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    making a variable final means that it can/will not be modified by the program. It allows the compiler to optimize some of the code.
    Also it tells anyone reading the program the variables are CONSTANTS. For example in a program you would define the value of PI as a constant and use that variable in your program instead of typing in 3.14... everywhere you needed to have the value.

  3. #3
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default

    ahh thank you very much :)

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    I suggest you always use final when there's no need to change the value of the variable. This because it makes your program faster and, especially with the larger projects, speed can be essential.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default

    duly noted and thx for the help

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also if you want to define a variable as a constant value in class level, not in local scope, define it as a final static variable.

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Only make it static if it's needed (if you want to call that method/variable from outside the class without having to instantiate the class) ;)
    I die a little on the inside...
    Every time I get shot.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Supamagier View Post
    Only make it static if it's needed (if you want to call that method/variable from outside the class without having to instantiate the class) ;)
    So you can use it(method/variable) with class name. ;)

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Yea, for example:
    Java Code:
    TestClass t = new TestClass();
    t.callMethod();
    In this case there's no need for 'callMethod' to be static, but here there is:
    Java Code:
    TestClass.callMethod();
    I die a little on the inside...
    Every time I get shot.

  10. #10
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default

    ahh I see thx :D

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    It's much simpler. Actually if you are use an instance of a class, new Java IDEs warning about that, and if you want simply neglect it and go ahead.

    At the same time, if you reduce at least a single line in your code, using these kind of tips, really nice.

  12. #12
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Final int

    Making a variable as final means that variable value can never be modified. It is considered as constant in Java and its value remains the same throughout. If modified java gives compilation error

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Final int

    Quote Originally Posted by kumar.bhatia18 View Post
    Making a variable as final means that variable value can never be modified. It is considered as constant in Java and its value remains the same throughout. If modified java gives compilation error
    This very old question has already been correctly answered. Let's stick to new threads. Locking thread.

Similar Threads

  1. UJO Framework 0.73.final
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-15-2008, 06:53 PM
  2. OpenMeetings 0.5 Final
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-01-2008, 04:13 PM
  3. UJO Framework 0.72.final
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-17-2008, 08:28 PM
  4. Scala 2.7.0-final
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-01-2008, 10:30 PM
  5. Poi 3.0-final
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-22-2007, 07:05 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
  •