Can class object be declared as final?
class TestIntB{
int a;
int b;
TestIntB(){
}
public void setA(int a){
this.a=a;
}
public int getA(){
return a;
}
}
class TestC{
TestC(TestIntB obj){
TestIntB test;
test=obj;
}
}
class TestInt{
public static void main(String[] args){
final TestIntB tInt = new TestIntB();
TestC tc = new TestC(tInt);
tInt.setA(10);
System.out.println("for final:");
TestIntB t1 = new TestIntB();
t1.setA(30);
System.out.println("values: "+t1.getA());
}
}
can a java class object be used with final? and can we change the final object value?
In the above program...in the main method, i am creating a final class object and now i am passing that as an argument to one of the method. so we are changing the final object value and assigning to another object.. is it really possible? can any one explain about this?
[981 byte] By [
spicysipa] at [2007-11-26 14:49:40]

> can a java class object be used with final?yes.> and can we change the final object value?no.
> > and can we change the final object value?> > no.Sure you can, just not the reference.
> Sure you can, just not the reference.final int k = 0;Change the value of that int.
you mis understand
public object Bob {
private int age;
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
final Bob bob = new Bob();
bob.setAge(99); // altering objects properties is ok
bob = new Bob(); // altering the reference is not ok
}
Ted.
> final int k = 0;> Change the value of that int.Ints are not objects.
> you mis understandno you misunderstand.> // altering the reference is not okoh, so 'bob' is a reference? so objects are pass by reference? cool. hey jverd ... !
> > // altering the reference is not ok
>
> oh, so 'bob' is a reference? so objects are pass by
> reference? cool. hey jverd ... !
Ummm that has nothing to do with this discussion. The point is you can manipulate the values contained in an object if the class provides mutators to do so. You cannot though change the reference of a final variable.
> > you mis understand> > no you misunderstand.> I do? O! I thought you could change a classes properties. Guess I'll have to rewrite all my code.Ted.
> The point is you can manipulate the values contained in an object if the class
> provides mutators to do so. You cannot though change the reference of a final
> variable.
well yes, but it's the "value" of the object you can't change; not it's reference. the value of an object _is_ a reference. it's not the reference you can't change, it's the value. semantics? or i'm out of it i guess. ok.
> > you mis understand
>
> no you misunderstand.
No, you really misunderstand.
> > // altering the reference is not ok
>
> oh, so 'bob' is a reference? so objects are pass by
> reference? cool. hey jverd ... !
What does that have to do with anything? Read Ted_Trippin's example, it alters a final object.
> Ummm that has nothing to do with this discussion. The
> point is you can manipulate the values contained in
> an object if the class provides mutators to do so.
> You cannot though change the reference of a final
> variable.
Mr.Cotton are you interest to join this java fight....
> well yes, but it's the "value" of the object you
> can't change;
Really? You didn't think that 3 minutes ago.
> and can we change the final object value?
no.
> not it's reference. the value of an
> object _is_ a reference.
Not really.
> What does that have to do with anything? Read> Ted_Trippin's example, it alters a final object.it alters some property of a reference pointed to by a final objects value. so what?
> > Ummm that has nothing to do with this discussion.
> The
> > point is you can manipulate the values contained
> in
> > an object if the class provides mutators to do so.
> > You cannot though change the reference of a final
> > variable.
>
> Mr.Cotton are you interest to join this java fight....
What?
> > well yes, but it's the "value" of the object you
> > can't change;
>
> Really? You didn't think that 3 minutes ago.
yes i did.
>
> > and can we change the final object value?
>
> no.
wow, we're learning!
> > not it's reference. the value of an
> > object _is_ a reference.
>
> Not really.
oh, please clarify?
> > > and can we change the final object value?> > > > no.> > wow, we're learning!Wow, you're more of an idiot than I thought you were. I'm leaving now.
What is do you mean by "final object" and "final object value"?
I've not come across the term "final object" before (but that does not mean it does not exist).
You have a final variable. This means the value of the variable can not be change. When thinking in the terms of objects the "value of the variable" is a reference to the object in the heap. The object can be manipulate the object.
You can have a final class, this simply means you can not extend the class.
mlka at 2007-7-21 16:18:45 >

> What is do you mean by "final object" and "final
> object value"?
>
> I've not come across the term "final object" before
> (but that does not mean it does not exist).
>
> You have a final variable. This means the value of
> the variable can not be change. When thinking in the
> terms of objects the "value of the variable" is a
> reference to the object in the heap. The object can
> be manipulate the object.
>
> You can have a final class, this simply means you can
> not extend the class.
mr mlk,
could you compare and contrast the above if
a) the object is an ArrayList
b) the object is a Vector
> > > Ummm that has nothing to do with this
> discussion.
> > The
> > > point is you can manipulate the values contained
> > in
> > > an object if the class provides mutators to do
> so.
> > > You cannot though change the reference of a
> final
> > > variable.
> >
> > Mr.Cotton are you interest to join this java
> fight....
>
> What?
What is what?
They already know final value can't be changed.. why your interuped them
ggopia at 2007-7-21 16:18:45 >

> could you compare and contrast the above if
>
> a) the object is an ArrayList
>
> b) the object is a Vector
What difference would it make? The internal state of the object is manipulatable, but the reference to the object is not.
Please keep the mlk thread in the mlk thread.
mlka at 2007-7-21 16:18:45 >

> > > > Ummm that has nothing to do with this
> > discussion.
> > > The
> > > > point is you can manipulate the values
> contained
> > > in
> > > > an object if the class provides mutators to do
> > so.
> > > > You cannot though change the reference of a
> > final
> > > > variable.
> > >
> > > Mr.Cotton are you interest to join this java
> > fight....
> >
> > What?
>
>
>
> What is what?
>
> They already know final value can't be changed.. why
> your interuped them
Please put down the crackpipe.
> oh, so 'bob' is a reference? Yes.> so objects are pass by reference?No.~
> > > > > Ummm that has nothing to do with this
> > > discussion.
> > > > The
> > > > > point is you can manipulate the values
> > contained
> > > > in
> > > > > an object if the class provides mutators to
> do
> > > so.
> > > > > You cannot though change the reference of a
> > > final
> > > > > variable.
> > > >
> > > > Mr.Cotton are you interest to join this java
> > > fight....
> > >
> > > What?
> >
> >
> >
> > What is what?
> >
> > They already know final value can't be changed..
> why
> > your interuped them
>
> Please put down the crackpipe.
crackpipe means?
ggopia at 2007-7-21 16:18:45 >

> > oh, so 'bob' is a reference?
>
> Yes.
you realise that this is why dafei thinks object 'pass by reference' right? at least one of the base reasons. he's also crazy.
> > so objects are pass by reference?
>
> No.
obviously.
> > > > > Ummm that has nothing to do with this
> > > discussion.
> > > > The
> > > > > point is you can manipulate the values
> > contained
> > > > in
> > > > > an object if the class provides mutators to
> do
> > > so.
> > > > > You cannot though change the reference of a
> > > final
> > > > > variable.
> > > >
> > > > Mr.Cotton are you interest to join this java
> > > fight....
> > >
> > > What?
> >
> >
> >
> > What is what?
> >
> > They already know final value can't be changed..
> why
> > your interuped them
>
> Please put down the crackpipe.
crackpipe means?
ggopia at 2007-7-21 16:18:45 >

> you realise that this is why dafei thinks object> 'pass by reference' right? I think there's more to his confusion* than that.* "confusion", "trolling"; not much difference, in this case.~