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]
# 1
> can a java class object be used with final?yes.> and can we change the final object value?no.
prob.not.sola at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 2
> > and can we change the final object value?> > no.Sure you can, just not the reference.
CaptainMorgan08a at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 3
> Sure you can, just not the reference.final int k = 0;Change the value of that int.
prob.not.sola at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 4

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.

ted_trippina at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 5
> final int k = 0;> Change the value of that int.Ints are not objects.
CaptainMorgan08a at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 6
> 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 ... !
prob.not.sola at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 7

> > // 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.

cotton.ma at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 8
> > 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.
ted_trippina at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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.

prob.not.sola at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 10

> > 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.

CaptainMorgan08a at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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....

ggopia at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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.

CaptainMorgan08a at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 13
> 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?
prob.not.sola at 2007-7-8 8:37:41 > top of Java-index,Java Essentials,Java Programming...
# 14

> > 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?

cotton.ma at 2007-7-8 8:37:42 > top of Java-index,Java Essentials,Java Programming...
# 15

> > 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?

prob.not.sola at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 16
> > > 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.
CaptainMorgan08a at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 17

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 > top of Java-index,Java Essentials,Java Programming...
# 18

> 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

prob.not.sola at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 19

> > > 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 > top of Java-index,Java Essentials,Java Programming...
# 20

> 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 > top of Java-index,Java Essentials,Java Programming...
# 21

> > > > 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.

cotton.ma at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 22
> oh, so 'bob' is a reference? Yes.> so objects are pass by reference?No.~
yawmarka at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 23
prob.not.sol has mpd.
shoopy.a at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 24

> > > > > 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 > top of Java-index,Java Essentials,Java Programming...
# 25

> > 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.

shoopy.a at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...
# 26

> > > > > 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 > top of Java-index,Java Essentials,Java Programming...
# 27
> 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.~
yawmarka at 2007-7-21 16:18:45 > top of Java-index,Java Essentials,Java Programming...