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


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

Re: Overloading methods question

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Overloading methods question
Date 2018-10-27 19:38 -0400
Organization Aioe.org NNTP Server
Message-ID <pr2sui$me8$1@gioia.aioe.org> (permalink)
References (7 earlier) <pqtnjm$abo$1@news.albasani.net> <pquv2d$6p2$1@dont-email.me> <pqvq6t$cah$1@news.albasani.net> <pr0d83$r6t$1@gioia.aioe.org> <pr2pa5$sql$1@news.albasani.net>

Show all headers | View raw


On 10/27/2018 6:36 PM, Martin Gregorie wrote:
> On Fri, 26 Oct 2018 20:58:44 -0400, Arne Vajhøj wrote:
>> On 10/26/2018 3:33 PM, Martin Gregorie wrote:
>>>                                         which is why I thought it
>>> could not be declared inside a class definition.
>>
>> You can put a class declaration inside a class.
>
> Yes indeed, but as I found out it seems not so simple to access it from
> outside. The following shows how I wanted to declare and use a enum, i.e.
> as a direct a replacement for the set of public static final ints).
> Here's an SSCCE that shows the problem.
> 
> public class EnumWrapper
> {
>     public enum MyEnum { ONE, TOO, TREE };

>     public static void printVal(MyEnum v)
>     {
>        System.out.println("printval(" + v + ")");
>     }
> }
> 
> This works as expected with the enum used inside its declaring class:

> BUT, I also want to use it as a parameter when calling a methods in the
> class where the enum is declared, IOW exactly how I'd use a public static
> final int declared in the class:
> 
> public class TestRun
> {
>     public static void main(String args[])
>     {
>        EnumWrapper ew = new EnumWrapper();
> 
>        ew.printval(MyEnum.ONE);
>     }
> }
> 
> ... and this won't compile:

 > and, if I replace 'MyEnum' with "EnumWrapper", the name of the class,
 > it still fails to compile:

Normal name rules aka same as for nested classes.

public class TestRun
{
    public static void main(String args[])
    {
       EnumWrapper ew = new EnumWrapper();
       ew.printVal(EnumWrapper.MyEnum.ONE);
    }
}

should work.

import EnumWrapper.MyEnum;

public class TestRun2
{
    public static void main(String args[])
    {
       EnumWrapper ew = new EnumWrapper();
       ew.printVal(MyEnum.ONE);
    }
}

should also work.

The enums full name is EnumWrapper.MyEnum so either
full name or an import to use short name.

(in real life there would also be a package, but ...)

Arne

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


Thread

Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-24 17:04 -0400
  Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-24 18:56 -0400
  Re: Overloading methods question Joerg Meier <joergmmeier@arcor.de> - 2018-10-25 10:13 +0200
    Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-25 15:53 -0400
      Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-25 13:58 -0700
        Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-25 17:49 -0400
          Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-25 22:29 +0000
            Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-25 20:08 -0400
              Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-26 00:37 +0000
                Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-25 21:42 -0400
                Re: Overloading methods question Joerg Meier <joergmmeier@arcor.de> - 2018-10-26 12:46 +0200
                Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-26 07:50 -0400
                Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-26 19:33 +0000
                Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-26 13:13 -0700
                Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-26 20:58 -0400
                Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-27 22:36 +0000
                Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-27 19:38 -0400
                Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-28 10:39 +0000
                Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-28 07:54 -0400
          Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-26 06:26 -0700
      Re: Overloading methods question Joerg Meier <joergmmeier@arcor.de> - 2018-10-26 12:52 +0200
        Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-26 22:10 -0400
          Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-27 07:43 -0400
            Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-29 14:40 -0400
              Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-29 16:39 -0400
          Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-29 05:07 -0700
            Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-29 14:41 -0400
  Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-25 07:49 -0400
    Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-25 16:07 -0400

csiph-web