> Hi,
>
> I am doing some scientific programming using Java. I have a static
> method doing some calculation. To help its calculation, I hope to have a
> class Helper inside the method doing some routine, modular calculation.
> But I ran into 2 problems. Could you help me out?
Probably not, but I'll give it a try.
> Thank you very much.
>
[quoted text clipped - 10 lines]
> MyClass obi", why? Because inside the method, obj needs to be
> modified/updated, adding final will prevent it be changed or not?
You need final here because the java specs demand it.
> {
> class Helper //Question 2: I hope it can be static, so I can
> Helper.getDiff(), instead of new Helper().getDiff(). But my compiler
> won't let me do it.
It can't be static because it is inside a member. If you want static
you could create Helper outside a member (either as in inner class or
as its own toplevel class) with a getDiff method like this - public
static double getDiff(MyClass obj)...
> {
> public static double getDiff()
[quoted text clipped - 11 lines]
>
> }
Shawn - 13 Oct 2006 14:30 GMT
>> ==============Subroutines.java===========
>> class MyClass
[quoted text clipped - 10 lines]
>
> You need final here because the java specs demand it.
Thank you. Just to make sure, so final doesn't mean obj cannot be
changed, correct? You know, usually final means it cannot be changed.
I sort of know the reason of adding final: when compiler goes into the
local inner class Helper (the stack), compiler cannot reach other stack
variables now , including obj. "final" makes obj copied somewhere else,
so at the stack of Helper, obj is still accessible.
Ian Wilson - 13 Oct 2006 15:10 GMT
> Thank you. Just to make sure, so final doesn't mean obj cannot be
> changed, correct? You know, usually final means it cannot be changed.
AIUI in this context, "final" means that, once assigned, the value of
the variable cannot be changed to refer to a different object. However
the object that the variable refers to can have it's internal state
altered e.g. by using it's methods.
e.g. this is OK:
final Foo foo;
...
foo = new Foo();
...
foo.setText("Foo");
...
foo.setText("Bar");
This is not OK:
final Foo foo = new Foo("Foo");
...
foo = new Foo("Foo");
Furious George - 13 Oct 2006 15:14 GMT
> >> ==============Subroutines.java===========
> >> class MyClass
[quoted text clipped - 13 lines]
> Thank you. Just to make sure, so final doesn't mean obj cannot be
> changed, correct? You know, usually final means it cannot be changed.
Of course, obj can be "changed." Since you made members A and B
public, there is nothing stopping you from doing obj.A++ or obj.B--.
OTH, the final prevents you from doing obj=new MyClass().
> I sort of know the reason of adding final: when compiler goes into the
> local inner class Helper (the stack), compiler cannot reach other stack
> variables now , including obj. "final" makes obj copied somewhere else,
> so at the stack of Helper, obj is still accessible.
You are thinking about this too much. "final" is just some garbage
text you put in your source code in order to trick the computer to do
what you want it to do. There is no reason other than that it is
required by the rules of the language. You should not worry about what
the compiler does or how it does it.