I have to declare a parameter on my method final to access it through a Runnable but can I still access the methods? I need to edit the object a bit. I can't seem to find anything that can help me with this question, so hopefully this isn't a stupid question. Thanks in advanced!
Asked
Active
Viewed 55 times
3 Answers
3
An object is not final
, but its reference is. So you can easily access its methods (if any) to modify the object.

Juvanis
- 25,802
- 5
- 69
- 87
-
1I'd actually say it's the *variable* which is final - you can't change the value of the variable to be a different reference. But this is still helpful as-is :) – Jon Skeet Apr 27 '13 at 08:05
0
You can change the state of the object, even if it is marked final
. When you mark a reference variable final
, you can't reassign it to another object, but you can definitely change the state of the object to which it is already referring by calling its methods.

Rahul Bobhate
- 4,892
- 3
- 25
- 48
0
Yes you can. Check this example
public class SampleA
{
private static final SampleB sampleB = new SampleB();
public static void main(String[] args)
{
System.out.println( sampleB.toString() );
sampleB.setM1( "1" );
System.out.println( sampleB.toString() );
}
}
public class SampleB
{
private String m1;
private String m2;
public String getM1()
{
return m1;
}
public void setM1(String m1)
{
this.m1 = m1;
}
public String getM2()
{
return m2;
}
public void setM2(String m2)
{
this.m2 = m2;
}
public String toString()
{
final String TAB = " ";
String retValue = "SampleB ( "
+ "m1 = " + this.m1 + TAB
+ "m2 = " + this.m2 + TAB
+ " )";
return retValue;
}
}

gurvinder372
- 66,980
- 10
- 72
- 94