Results 1 to 5 of 5

Thread: Final keyword in Java

  1. #1
    Join Date
    Nov 2009
    Posts
    862

    Final keyword in Java

    Hi, I want some example code and basic explanation of final keyword in JAVA. Can anyone provide me this? I am trying to improve my knowledge in java and want to learn new things. I am not having more knowledge about java. So, please give me details of final keyword.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Final keyword in Java

    Hi, Final keyword is used in java for the purpose of making some housekeeping task. There may be possibility that your try catch block is not get executed but your final block gets executed at every time when your program would get executed. Therefore this block is used for making those executions which have to be done before moving out of the program. As whether your program gets exception or not, it has to execute the final block. That is why many times final block is used to close the connections, so no single connection is remain open to slow down the traffic of the two nodes.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Final keyword in Java

    Hi, you have not explained where did you want to use the final keyword. If a method is declared final then subclasses would be prevented from overriding or hiding it. It will give compile-time error. You can't declare them abstract.

    It can be used as follows:

    Code:
    final class one 
    {
    int a, b;
    void change (int x, int y) { a += x; b += y; }
    }
    class check
    {
    public static void main(String[] args) 
    {
    test[] p = new test[100];
    for (int i = 0; i < p.length; i++) 
    {
    p[i] = new test();
    p[i].change(i, p.length-1-i);
    }
    }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Final keyword in Java

    Hi, you want to know about keyword final in java, right? In java basically, final simply means that the value will not going to change further.

    Advantages:
    • It is used only when you know that the value is not going to be changed further.
    • It wont allow anyone to change that value further in anyway.
    • It helps the compiler to generate code faster.

    For example:
    // final is used to ensure a variable is always assigned a value,
    // and once assigned value never change.

    Code:
    final int a;
    if ( x> 0 )
       {
       a=14;
       }
    else if ( x < 0 )
       {
       a = 0;
       }
    else
       {
      a = 7;
       }

  5. #5
    Join Date
    Oct 2011
    Posts
    1

    Re: Final keyword in Java

    You can find more information on all different uses of final here https://sites.google.com/site/javadvanced/final-keyword

Similar Threads

  1. Equivalent to Java finally keyword in PHP
    By Jaganmohini in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:20 AM
  2. Keyword Analyzer and keyword ranking tool
    By Captain Carrot in forum Technology & Internet
    Replies: 5
    Last Post: 21-06-2010, 03:12 PM
  3. How to use same keyword for two purposes in java?
    By Juaquine in forum Software Development
    Replies: 4
    Last Post: 08-03-2010, 09:48 PM
  4. Difference between final, finally and finalize in Java?
    By Maddox G in forum Software Development
    Replies: 6
    Last Post: 31-01-2010, 11:09 AM
  5. Replies: 3
    Last Post: 09-01-2009, 01:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,001,949.86026 seconds with 17 queries