Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35564

Re: Changing accessor on public static class

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Changing accessor on public static class
Date 2016-09-07 18:52 -0400
Organization Aioe.org NNTP Server
Message-ID <nqq5nk$vad$1@gioia.aioe.org> (permalink)
References <eb67e063-898b-4580-9c6f-18a8dae4d95a@googlegroups.com> <dd57e08f-04ae-4858-940c-9d2430212265@googlegroups.com> <nqnjl3$13g4$3@gioia.aioe.org> <734645c0-69c9-4ee2-a9c2-191b9f3710ab@googlegroups.com>

Show all headers | View raw


On 9/7/2016 7:47 AM, Eric Douglas wrote:
> On Tuesday, September 6, 2016 at 7:31:56 PM UTC-4, Arne Vajhøj
> wrote:
>> No. It is quite common to have static nested classes used in
>> non-static methods.
>
> I don't know how common it is but it's not necessary.  You can create
> an instance of a static class in a non static method but you can't
> create an instance of a non static class in a static method, so
> whether or not you instantiate it in a non static method, the point
> of making the class static is so you can instantiate it in a static
> method.

No no no.

A static nested class is a relative normal class just declared
within another class. It is often a good choice when the enclosing
class needs to return something implementing an interface or
similar. The same concept is known from both C# and C++ (they
don't use the static keyword, but a nested class without static in
those languages is the same as a static nested class in Java).

An inner class (non-static nested class) is a very complex
construct with its capture or automatic reference to the
creating instance. If you don't need this specific
feature then don't use this type of class. I don't think
I have ever had that need (not counting the closely
related anonymous inner class, which was very useful
before lambdas and still are somewhat useful).

So your decision logic should be:

if I want to put class declaration within another class then
     if the class really need to refer directly to members of the 
enclosing instance then
         use inner class
     else
         use static nested class
     end if
else
     use a normal class
end if

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Changing accessor on public static class mike <mikaelpetterson@hotmail.com> - 2016-09-06 00:53 -0700
  Re: Changing accessor on public static class Alberto Salvati <zzzato@gmail.com> - 2016-09-06 01:12 -0700
  Re: Changing accessor on public static class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-06 09:08 -0400
    Re: Changing accessor on public static class Andreas Leitgeb <avl@logic.at> - 2016-09-06 23:00 +0000
      Re: Changing accessor on public static class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-07 07:48 -0400
  Re: Changing accessor on public static class Eric Douglas <e.d.programmer@gmail.com> - 2016-09-06 07:00 -0700
    Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-06 19:31 -0400
      Re: Changing accessor on public static class Eric Douglas <e.d.programmer@gmail.com> - 2016-09-07 04:47 -0700
        Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-07 18:52 -0400
          Re: Changing accessor on public static class Eric Douglas <e.d.programmer@gmail.com> - 2016-09-08 04:54 -0700
            Re: Changing accessor on public static class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-08 08:38 -0400
              Re: Changing accessor on public static class Eric Douglas <e.d.programmer@gmail.com> - 2016-09-08 06:35 -0700
                Re: Changing accessor on public static class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-08 10:09 -0400
                Re: Changing accessor on public static class Eric Douglas <e.d.programmer@gmail.com> - 2016-09-08 08:18 -0700
                Re: Changing accessor on public static class Eric Douglas <e.d.programmer@gmail.com> - 2016-09-08 08:37 -0700
                Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-08 19:34 -0400
                Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-08 19:32 -0400
            Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-08 19:30 -0400
              Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-08 21:50 -0400
            Re: Changing accessor on public static class Lew <lewbloch@gmail.com> - 2016-09-13 17:44 -0700
    Re: Changing accessor on public static class Lew <lewbloch@gmail.com> - 2016-09-13 17:42 -0700
  Re: Changing accessor on public static class Arne Vajhøj <arne@vajhoej.dk> - 2016-09-06 19:33 -0400
  Re: Changing accessor on public static class Lew <lewbloch@gmail.com> - 2016-09-13 17:39 -0700

csiph-web