• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

how...final works..

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

program no:1
___________________
public class testfinal{
public static void main(String args[]){
final int i = 5;
System.out.println(++i);
}
}
________________________
program no:2
_________________________
public class testfinal{
public static void main(String args[]){
final int i[] = {5};
System.out.println(++i[0]);
}
}
_________________________

here both the program 1 and 2 is not supposed to work because we have declared i as final. but program 2 works fine without any complainat and prints the reult as 6...why???
help me to understand it
thanx....
jayanthi
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jayanthi,
When you declare
final int[] =... you are telling compiler that the reference to integer array to be final. That does not mean the value of the object can not be changed. It only means that the array reference can not be reassigned to another reference.
supposing you had another array like int []j=....
i=j;
or i=null; becomes invalid statements.
But you can certainly assign i[0]=100;
Does that clear your doubt?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic