users
[Top] [All Lists]

RE: [cinjug-users] Testing (Inheritance?) question...

To: <users@xxxxxxxxxx>
Subject: RE: [cinjug-users] Testing (Inheritance?) question...
From: "James Carman" <james@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 31 Aug 2004 11:27:41 -0400
Delivered-to: mailing list users@cinjug.org
Importance: Normal
In-reply-to: <7782045.1093955125988.JavaMail.ansapp@brunch.mit.edu>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Organization: Carman Consulting, Inc.
Rob, you are attempting to do something that cannot be done.  The reason
that you don't need to have some sort of "class context" (similar to the
such as getClass() method) within a static method is that static methods
cannot be overridden.  They can only be "hidden."  You could hide the main
method with a similar one, substituting in an instance of the B class where
you previously used an instance of the A class.  Of course, you would
extract out a method for the duplicate code as follows...  


public class A
{
    public static void main( String[] args )
    {
        startup( new A() );
    }

    protected static void startup( A a )
    {
        a.run();
    }

    public void run()
    {
        System.out.println( "You're running an A object." );
    }
}

public class B extends A
{
    public static void main( String[] args )
    {
        startup( new B() );
    }

    public void run()
    {
        System.out.println( "You're running a B object." );
    }
}


-----Original Message-----
From: Rob Biedenharn [mailto:rob_biedenharn@xxxxxxxxxxxx] 
Sent: Tuesday, August 31, 2004 8:25 AM
To: users@xxxxxxxxxx
Subject: RE: [cinjug-users] Testing (Inheritance?) question...

Mark,
That's essentially what I could come up with, but without the need for a
Factory.  If main() ceases to do anything that could break :-), then I can
just call B.mainImpl() and mainImpl() can call this.run().  If I already
have an instance, there's no need to call new A() (or new B(), etc.)
anymore.

Can I assume that your response is confirmation that there's no way for a
static method to know if it is being invoked by a derived class?

-Rob

-----Original Message-----
From: Mark Windholtz [mailto:lists@xxxxxxxxxxxxxx]
Sent: Tuesday, August 31, 2004 8:13 AM
To: users@xxxxxxxxxx
Subject: Re: [cinjug-users] Testing (Inheritance?) question...

Rob,

You answered the question yourself.
I sometimes joke that the "static" keyword should be renamed 
"hard-to-test" so that when
a programmer types it they know the consequences.

You could have your main() , immediately forward everything to an 
instance method  perhaps:
public void mainImpl(String[] args)

Then use a Method Factory override to plug the "new A()" with a mock if 
needed.

Later,
-Mark.

On Aug 30, 2004, at 2:21 PM, Rob Biedenharn wrote:

>
> P.S.  I know that I can equivalently refactor the "blah, blah, blah" 
> and "arg checking" into separate functions and then test /those/, but 
> the original idea has me curious.
>
> A.java
> ======
> public class A {
>  public static void main(String[] args) {
>      // blah, blah, blah,
>      // arg checking
>      if (noErrors) {
>       (new A()).run(moreArgs, otherArgs);
>      }
>   }
>
>   public void run(String someArg, String someOtherArg) {
>     // yada, yada, yada
>   }
> }
>
>
> ATest.java
> ======
> public B extends A {
>   public void run(String someArg, String someOtherArg) {
>     this.someArg = someArg;
>     this.someOtherArg = someOtherArg;
>   }
>   String someArg;
>   String someOtherArg;
> }

Regards,
- Mark Windholtz
(513) 226-8259
www.objectwind.com




<Prev in Thread] Current Thread [Next in Thread>