question on how to use local inner class inside a static method

S

Shawn

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? Thank you very much.


==============Subroutines.java===========
class MyClass
{
public double A, B;
}

public class Subroutines
{

public static double getValue(MyClass obj) //Question 1: I need "final
MyClass obi", why? Because inside the method, obj needs to be
modified/updated, adding final will prevent it be changed or not?
{
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.
{
public static double getDiff()
{
retunr (obj.A-obj.B);

}
}

//start doing calculation
Helper helper = new Helper();
...//code for doing calculation. I simplified here in the posting
return helper.getDiff();
}

}
 
F

Furious George

Shawn said:
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.


==============Subroutines.java===========
class MyClass
{
public double A, B;
}

public class Subroutines
{

public static double getValue(MyClass obj) //Question 1: I need "final
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)...
 
S

Shawn

Furious said:
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.
 
I

Ian Wilson

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");
 
F

Furious George

Shawn said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top