Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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?
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic