Path: csiph.com!weretis.net!feeder4.news.weretis.net!news.albasani.net!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: Overloading methods question Date: Sat, 27 Oct 2018 22:36:53 +0000 (UTC) Organization: albasani.net Lines: 93 Message-ID: References: <1b2bqd7oqzez6$.sfpve8lwmic2.dlg@40tude.net> <75ad8b55-3def-4ec1-b46b-65ecd971d0fe@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net ELX4Z4DMVJZCttQ3mELR/J6Y2RAa2cUP5zFppuagO0TFpLkvTdBlenY22xRalTOM50x3+hs8HVbzQXBnmkHIPLFp8jeKjvTXPwIZh2Hvg8wZNoMTl3QjEUE40ZnS60L7 NNTP-Posting-Date: Sat, 27 Oct 2018 22:36:53 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="dN0T+lAnTT753iLYFqju+F62lcNnYDKL6PHuwEE+5pd7SSH541auZH8DE3fFeIQaVWcgWnegUDW8XRg7CjH8zeel+JVQySKGYBGhUAWKoT+ahdAAay7OsRAv2Denyu8o"; mail-complaints-to="abuse@albasani.net" User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a git.gnome.org/pan2) Cancel-Lock: sha1:VoaxYDS+EKYrNoIMpsTeSDsq9eE= Xref: csiph.com comp.lang.java.programmer:38667 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