Re: Confused about scope
Paul King <[email protected]> Sat, 28 Sep 2024 13:25:46 +1000
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <CAMbkE7TDf6+Yc24nUn6rtPmQ1qs=z4jt8LPJHcuA4uaD0RR7NA@mail.gmail.com> |
In terms of generated bytecode, Java and Groovy are the same with the following exceptions: * for methods and classes, Groovy defaults to public visibility if you provide no visibility modifier (that is what Intellij is telling you) * for fields, Groovy turns a field into a property if there is no visibility modifier * Java defaults to package-private visibility, you have to use @PackagePrivate in Groovy for this rarer case In terms of obeying scoping, both Java and Groovy let you get to private members through reflection in earlier JDKs. Groovy provides nicer syntax for this when using dynamic mode but you shouldn't rely on that since it has been essentially turned off for later JDKs. Cheers, Paul. On Sat, Sep 28, 2024 at 5:04 AM Blake McBride <[email protected]> wrote: > > Greetings, > > Although I have been using Groovy for years, I have largely been ignoring scoping as it relates to classes, methods, and fields. It is my opinion that having private and package-level scoping is critical. Controlling scope is critical to managing a large project. > > The way I've been ignoring the issue is: > > 1. I just assume it's the same as Java and treat it that way. > > 2. Since my large project (~9,000 classes) is broken down into small packages, I also control visibility by not cross-accessing packages. > > (I understand the field property bit.) > > My problem is because of two factors: > > 1. The documentation that comes with Groovy is not explicit about this. > > 2. My IDE (IntelliJ) grays out "public" trying to tell me it isn't needed. > > So, I'd like to get a clear explanation of scope control for classes, methods, and fields. > > Thanks! > > Blake McBride > > >