Skip to main content
quote from the referenced link
Source Link
gnat
  • 20.5k
  • 29
  • 117
  • 310

Immutable means you can't change the value, and mutable means you can change the value if you think in terms of primitives, and objects. Objects are different than primitives in Java in that they are built in types. Primitives are built in types like int, boolean, and void.

A lot of people people think that primitives and objects variables that have a final modifier infront of them are immutable, however, this isn't exactly true. So final almost doesn't mean immutable for variables. Check out this linkethis link for a code sample http://www.searchconsort.com/h/D0000F.php.:

public abstract class FinalBase { private final int variable; // Unset /* if final really means immutable than * I shouldn't be able to set the variable * but I can. */ public FinalBase(int variable) { this.variable = variable; } public int getVariable() { return variable; } public abstract void method(); } // This is not fully necessary for this example // but helps you see how to set the final value // in a sub class. public class FinalSubclass extends FinalBase { public FinalSubclass(int variable) { super(variable); } @Override public void method() { System.out.println( getVariable() ); } @Override public int getVariable() { return super.getVariable(); } public static void main(String[] args) { FinalSubclass subclass = new FinalSubclass(10); subclass.method(); } } 

Immutable means you can't change the value, and mutable means you can change the value if you think in terms of primitives, and objects. Objects are different than primitives in Java in that they are built in types. Primitives are built in types like int, boolean, and void.

A lot of people people think that primitives and objects variables that have a final modifier infront of them are immutable, however, this isn't exactly true. So final almost doesn't mean immutable for variables. Check out this linke for a code sample http://www.searchconsort.com/h/D0000F.php.

Immutable means you can't change the value, and mutable means you can change the value if you think in terms of primitives, and objects. Objects are different than primitives in Java in that they are built in types. Primitives are built in types like int, boolean, and void.

A lot of people people think that primitives and objects variables that have a final modifier infront of them are immutable, however, this isn't exactly true. So final almost doesn't mean immutable for variables. Check out this link for a code sample:

public abstract class FinalBase { private final int variable; // Unset /* if final really means immutable than * I shouldn't be able to set the variable * but I can. */ public FinalBase(int variable) { this.variable = variable; } public int getVariable() { return variable; } public abstract void method(); } // This is not fully necessary for this example // but helps you see how to set the final value // in a sub class. public class FinalSubclass extends FinalBase { public FinalSubclass(int variable) { super(variable); } @Override public void method() { System.out.println( getVariable() ); } @Override public int getVariable() { return super.getVariable(); } public static void main(String[] args) { FinalSubclass subclass = new FinalSubclass(10); subclass.method(); } } 
Source Link
Jon
  • 9
  • 1

Immutable means you can't change the value, and mutable means you can change the value if you think in terms of primitives, and objects. Objects are different than primitives in Java in that they are built in types. Primitives are built in types like int, boolean, and void.

A lot of people people think that primitives and objects variables that have a final modifier infront of them are immutable, however, this isn't exactly true. So final almost doesn't mean immutable for variables. Check out this linke for a code sample http://www.searchconsort.com/h/D0000F.php.

Post Made Community Wiki by Jon