Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38667
| From | Martin Gregorie <martin@mydomain.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Overloading methods question |
| Date | 2018-10-27 22:36 +0000 |
| Organization | albasani.net |
| Message-ID | <pr2pa5$sql$1@news.albasani.net> (permalink) |
| References | (6 earlier) <pqtluk$gg9$1@gioia.aioe.org> <pqtnjm$abo$1@news.albasani.net> <pquv2d$6p2$1@dont-email.me> <pqvq6t$cah$1@news.albasani.net> <pr0d83$r6t$1@gioia.aioe.org> |
On Fri, 26 Oct 2018 20:58:44 -0400, Arne Vajhøj wrote:
> On 10/26/2018 3:33 PM, Martin Gregorie wrote:
>> I haven't (so far) used Enums and it wasn't obvious from the tutorial
>> stuff that you can put a public Enum inside a class - rather the
>> opposite in fact, since at one point it said that an enum declaration
>> was at a similar language level to a class declaration,
>
> It is.
>
>> 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 main(String args[])
{
for (MyEnum x: MyEnum.values())
printVal(x);
}
public static void printVal(MyEnum v)
{
System.out.println("printval(" + v + ")");
}
}
This works as expected with the enum used inside its declaring class:
$ java EnumWrapper
printval(ONE)
printval(TOO)
printval(TREE)
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:
This works as expected if the enum is being used inside its declaring
class:
$ javac TestRun.java
TestRun.java:7: error: cannot find symbol
ew.printval(MyEnum.ONE);
^
symbol: variable MyEnum
location: class TestRun
1 error
and, if I replace 'MyEnum' with "EnumWrapper", the name of the class,
it still fails to compile:
$ javac TestRun.java
TestRun.java:7: error: cannot find symbol
ew.printval(EnumWrapper.ONE);
^
symbol: variable ONE
location: class EnumWrapper
1 error
Moving the enum to a separate file containing nothing except a package
name specifier and the public enum declaration obviously works, but is
not what I want since it separates the constants in the declaration from
the code that accepts them as method arguments.
I understood, or thought I did, from earlier parts of this thread that it
should be possible to do what I want, i.e. declare a public enum within
the class whose methods are controlled by the enum values. I've checked
various sources on the interwebs including the Oracle tutorial, but
didn't find anything there, so - what have I missed or misunderstood?
--
Martin | martin at
Gregorie | gregorie dot org
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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