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 / January 2006

Tip: Looking for answers? Try searching our database.

Enabling assertions for inner class

Thread view: 
Ian Pilcher - 11 Jan 2006 19:48 GMT
I am attempting to use a static inner class for unit testing.  As part
its class initialization I want to check that assertions are enabled for
both the inner class and its enclosing class.

Here is an example:

package temp;

public class TestMe
{
   //  Stuff to test goes here

   private static boolean assertionsEnabled()
   {
       try
       {
           assert false;
           return false;
       }
       catch (AssertionError e)
       {
           return true;
       }
   }

   private static class __UnitTest__
   {
       //  Ensure assertions are enabled for this class and TestMe
       
       static
       {
           try
           {
               assert false;
               throw new Error("assertions not enabled");    // 29
           }
           catch (AssertionError e)
           {
               assert TestMe.assertionsEnabled();        // 33
           }
       }

       public static void main(String[] args)
       {
           //  Tests go here
       }
   }
}

When I run with assertions disabled, things go as expected:

[pilcher@home temp]$ java temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
       at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$

Turning all (non-system) assertions on also works as expected:

[pilcher@home temp]$ java -ea temp.TestMe\$__UnitTest__
[pilcher@home temp]$

Turning assertions on for the enclosing class seems to also enable them
for the inner class:

[pilcher@home temp]$ java -ea:temp.TestMe temp.TestMe\$__UnitTest__
[pilcher@home temp]$

Attempting to enable assertions for *only* the inner class does not
seem to work, however:

[pilcher@home temp]$ java -ea:temp.TestMe\$__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
       at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$ java -ea:temp.TestMe.__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
       at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$

Is this expected behavior?  If so, is it guaranteed?  (The latter would
be nice, because it would allow me to dispense with the
assertionsEnabled() method in the outer class.)

Thanks!

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Tony Morris - 11 Jan 2006 22:11 GMT
There is no such thing as a static inner class.
What you have shown is called a static nested class.
Inner classes are, by definition, not static.

JLS 14.10 quite explicitly answers your question.
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10

Signature

Tony Morris
http://tmorris.net/

ricky.clarkson@gmail.com - 13 Jan 2006 00:28 GMT
G'day Tony,

> What you have shown is called a static nested class.

And isn't the word 'static' redundant there, Tony?

Ricky.
Tony Morris - 13 Jan 2006 01:54 GMT
> G'day Tony,
>
[quoted text clipped - 3 lines]
>
> Ricky.

Hello Ricky :)
No there is no redundancy, you can have non-static nested classes i.e. inner
classes.

Signature

Tony Morris
http://tmorris.net/

ricky.clarkson@gmail.com - 13 Jan 2006 02:32 GMT
You're correct, I checked the JLS.  I had it in my head that nested was
static, and inner was non-static.

Sorry for the bother.
Ian Pilcher - 13 Jan 2006 15:37 GMT
> You're correct, I checked the JLS.  I had it in my head that nested was
> static, and inner was non-static.

Glad I'm not the only one who has trouble remembering which is which.

Let me see if I've got this right:

class Outer
{
   static class Nested {}
   class Inner {}
   class NonStaticNested {}
}

Seems needlessly confusing to me.

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

P.Hill - 14 Jan 2006 03:40 GMT
> Seems needlessly confusing to me.

Inner class is the slightly older term which was formalized in the JLS
with "nested" and "top-level" thus producing more than one common term
for the same thing.

-Paul
Roedy Green - 14 Jan 2006 05:25 GMT
On Fri, 13 Jan 2006 09:37:55 -0600, Ian Pilcher
<i.pilcher@comcast.net> wrote, quoted or indirectly quoted someone who
said :

>    static class Nested {}
>    class Inner {}
>    class NonStaticNested {}

see http://mindprod.com/jgloss/nestedclasses.html
and chase links to sort it all out.

Part of the problems is many people use the terms imprecisely.  I am
catching myself now.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Tony Morris - 13 Jan 2006 19:52 GMT
Hi Roedy,
You might want to also make it clear on your web page here:
# anonymous classes: unnamed inner class defined in the middle of a method.
# local classes: named inner class defined in the middle of a method.

The distinction between a local and anonymous class is not quite correct.
An anonymous class is a type of local class - the other type being a named
class.
Local class is to anonymous class as nested class is to inner class - one is
a more general type of the other.

Also, a method is not just the only context in which local classes can be
used.
Specifically, a method, constructor or initialiser are all possible contexts
of local (and therefore, anonymous) classes.

--
Tony Morris
http://tmorris.net/

Java Questions and Answers
http://jqa.tmorris.net/

> On Fri, 13 Jan 2006 09:37:55 -0600, Ian Pilcher
> <i.pilcher@comcast.net> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 13 lines]
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.
Chris Smith - 14 Jan 2006 06:05 GMT
> The distinction between a local and anonymous class is not quite correct.
> An anonymous class is a type of local class - the other type being a named
> class.

That's certainly an internally consistent use of terminology, but it is
not the same as the usage within the Java Language Specification...
which says:

§8
   "This chapter discusses the common semantics of all classes -- top
    level (§7.6) and nested (including member classes (§8.5, §9.5),
    local classes (§14.3) and anonymous classes (§15.9.5))."

§14.3
   "A local class is a nested class (§8) that is not a member of any
    class and that has a name."

The first statement implies, and the second states outright, that an
anonymous class does NOT qualify as a "local class" in language spec
words.  They certainly do share a lot of characteristics, though.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Tony Morris - 13 Jan 2006 20:27 GMT
Tony Morris <not@telling.you> wrote:
> The distinction between a local and anonymous class is not quite correct.
> An anonymous class is a type of local class - the other type being a named
> class.

That's certainly an internally consistent use of terminology, but it is
not the same as the usage within the Java Language Specification...
which says:

§8
   "This chapter discusses the common semantics of all classes -- top
    level (§7.6) and nested (including member classes (§8.5, §9.5),
    local classes (§14.3) and anonymous classes (§15.9.5))."

§14.3
   "A local class is a nested class (§8) that is not a member of any
    class and that has a name."

The first statement implies, and the second states outright, that an
anonymous class does NOT qualify as a "local class" in language spec
words.  They certainly do share a lot of characteristics, though.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Hello Chris,
I hadn't come across that section of the JLS. I can find other sections of
the JLS where there is clearly an implication that an anonymous class is a
local class, but certainly nothing as explicit as you mention. I put it down
to yet another contradiction in the JLS - and change my recommendation to
either stating the premise of the section of the JLS that you cite or change
the wording to avoid potential confusion.

I still think that it should not be explicitly stated that local/anonymous
classes are restricted to methods, since obviously, they are not.

Tony Morris
http://tmorris.net/
Java Questions and Answers
http://jqa.tmorris.net/


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



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