I had thought final in Java was like const in C++ sorta, or that was what I had wondered upon learning about const in C++.
I had heard that you might not want a value to be changed and so passed a final object as a parameter and I tested it by changing the value and it let me.
What's more, I tried making the whole method final and then calling it and using the returned (anonymous) object to call a set method and it let me do it.
I thought "final" meant it can't be changed.
/** * Write a description of class Example here. * * @author (your name) * @version (a version number or a date) */ public class Example { // instance variables - replace the example below with your own private int x; private String words; /** * Constructor for objects of class Example */ public Example(int x, String words) { setX(x); setWords(words); } public Example() { } public int getX() { return x; } public void setX(int x) { this.x = x; } public void setWords(String words) { this.words = words; } public String getWords() { return words; } // sets the values of this new Example object to the values of the parameter public final Example copy( final Example ex) { Example ex2 = new Example(ex.getX(), ex.getWords()); ex.setX(3); return (ex2); } public static void main(String[] args) { Example ex = new Example(5,"bob"); Example ex2 = new Example(4, "mike"); ex = ex.copy(ex2); ex.copy(ex2).setX(3); System.out.println(ex.getX()); } }
I had been trying to get it to see if there was a way in a clone method you could make it so the object you're cloning CAN'T be changed (by mistake) in the method.