Access modifiers
You can use access modifier keywords to set the level of access to a Gosu class, interface, enumeration, or a type member, which is a function, variable, or property. The access level determines whether other classes can use a particular variable or invoke a particular function.
For example, methods and variables marked
public are visible from
other classes in the package. Additionally, because they are public,
functions and variables also are visible to all subclasses of the class
and to all classes outside the current package. For example, the following
code uses the public access
modifier on a class variable:
package com.mycompany.utils
class Test1 {
public var Name : String
}
In contrast, the internal access modifier lets the variable be accessed only in the
same package as the class:
package com.mycompany.utils
class Test2 {
internal var Name : String
}
For example, another class with fully qualified name
com.mycompany.utils.Test2
could access the Name
variable because it is in the same package. Another class com.mycompany.integration.Test3
cannot see the Test.Name
variable because it is not in the same package.
Similarly, modifiers can apply to an entire type, such as a Gosu class:
package com.mycompany.utils
internal class Test {
var Name : String
}
Some access modifiers apply only to type members (functions, variables, properties, and inner types). Other access modifiers apply to both type members and top-level types (outer Gosu classes, interfaces, enumerations). The following table lists the Gosu access modifiers and their applicability and visibility:
Modifier |
Description |
Applies to top-level types |
Applies to type members |
Visible in class |
Visible in package |
Visible in subclass |
Visible to all |
|---|---|---|---|---|---|---|---|
|
Fully accessible with no restrictions |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
|
Accessible only to types in the same package and subtypes |
-- |
Yes |
Yes |
Yes |
Yes |
-- |
|
Accessible only in same package |
Yes |
Yes |
Yes |
Yes |
-- |
-- |
|
Accessible only in the declaring type, such as the Gosu class or interface that defines it |
-- |
Yes |
Yes |
-- |
-- |
-- |
If you do not specify a modifier, Gosu uses the following default access levels:
Element |
Default modifier |
|---|---|
Types and classes |
|
Variables |
|
Functions |
|
Properties |
|
Coding style recommendations for variables
Always prefix private and protected class variables with
an underscore character (_).
Avoid public variables. If you want to use public variables, convert the public variables to properties. This conversion separates the way code in other classes accesses the properties from the implementation of the storage and retrieval of the properties.
See also
