Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links



Final keyword in Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 24-11-2009
Level8's Avatar
Member
 
Join Date: Nov 2009
Posts: 873
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.
Reply With Quote
  #2  
Old 24-11-2009
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
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.
Reply With Quote
  #3  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 1,990
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);
}
}
}
Reply With Quote
  #4  
Old 24-11-2009
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
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;
   }
Reply With Quote
  #5  
Old 26-10-2011
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Final keyword in Java"
Thread Thread Starter Forum Replies Last Post
Equivalent to Java finally keyword in PHP Jaganmohini Software Development 6 12-08-2010 11:20 AM
Keyword Analyzer and keyword ranking tool Captain Carrot Technology & Internet 5 21-06-2010 04:12 PM
How to use same keyword for two purposes in java? Juaquine Software Development 4 08-03-2010 09:48 PM
Difference between final, finally and finalize in Java? Maddox G Software Development 6 31-01-2010 11:09 AM
How to use Search Engine Keyword Tracker & Keyword Ranking Code using PHP Burnet Software Development 3 09-01-2009 01:38 PM


All times are GMT +5.5. The time now is 07:43 AM.