This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final

 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Folks,

Could anyone please explain how the result is " true true "

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives the same output without the final keywords. So what's this got to do with final?
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFter the call to FizzSwitch(), both f3 and f1 are the same and both have the same value of 6 which got assigned in the switch method.

FizzSwitch() takes f1 as its first arg, and assigns the value of 6 by temporarily assigning it to z and returns f1.
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you declare an object final then it means that you can not change the reference to another object, but the objects state can be changed.

final Test o = new Test();
o= new Test(); //CANNOT BE DONE
o.x=9; //CAN BE DONE

where
public Test {
int x =0;
}

Also when you pass an argument to the method you pass a COPY OF THE REFERENCE.

so
//main method excerpt
Test a= new Test();
a.x=7;
Test q =method(a); //Now q.x value is 45 and a.x is 45
//if b=null is uncommented then q will be null and a.x will be 45


static Test method (Test a) {
Test b=a;
b.x=45;
//b=null;
return b;
}


Hope this gives you a headstart
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic