Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Overloading methods question Date: Sat, 27 Oct 2018 19:38:56 -0400 Organization: Aioe.org NNTP Server Lines: 77 Message-ID: References: <1b2bqd7oqzez6$.sfpve8lwmic2.dlg@40tude.net> <75ad8b55-3def-4ec1-b46b-65ecd971d0fe@googlegroups.com> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:38668 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