Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Shawn - 13 Oct 2006 14:02 GMT
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();
    }

}
Furious George - 13 Oct 2006 14:22 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.