Results 1 to 4 of 4
  1. #1
    tabrezbasha is offline Member
    Join Date
    Jun 2016
    Posts
    2
    Rep Power
    0

    Question Final vs Static variable

    Hello,
    I'm new to Java and I want to know a real world example where final variable will be used over static . Why to use final variable when we can have static variable only - one copy.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Final vs Static variable

    Final and static have nothing to do wit each other: you can't change a final variable and a static variable belongs to a class, not an object. Static final variables belong to a class and can't be changed ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    tabrezbasha is offline Member
    Join Date
    Jun 2016
    Posts
    2
    Rep Power
    0

    Default Re: Final vs Static variable

    Hi JosAH,

    Thanks for your response. For static variable there is a very good example of Interest rate which is a fixed component and hence this variable will be declared as static.
    As this is fixed and will not be changed, i understand that we cannot declare this variable as final since if we do so it will be created for every object and hence wastage of memory.

    Now im looking for one such real scenario where final will be dominating than static variable.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Final vs Static variable

    If that variable shouldn't change and it is the same for all objects of that class, you should make it a static final variable.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 8
    Last Post: 03-01-2016, 10:25 AM
  2. Replies: 6
    Last Post: 06-30-2015, 06:19 PM
  3. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Tags for this Thread

Posting Permissions

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