80 likes | 233 Views
CSCI 224. Scope – Package – Access. Scope. Scope refers to accessing members (including inherited ones) from code within a class. Class scope and block scope While there are explicit modifiers in Java to control access there are some specific rules that also apply. Golden Rule of Scope.
E N D
CSCI 224 Scope – Package – Access
Scope • Scope refers to accessing members (including inherited ones) from code within a class. • Class scope and block scope • While there are explicit modifiers in Java to control access there are some specific rules that also apply.
Golden Rule of Scope • That static code cannot access non-static code by their simple names. • References this and super are not available. • An object has knowledge of it’s class, therefore static members are always accessible in a non-static context.
What is static code • Static members belong to the class, not an instance of the class • Class variables • Class methods • When a class is first initialized, the static members are created once. • Ie. Public static void main (String[] args) • This applies also to static initializer blocks also (to be discussed later)
Variables and Scope • Class variables • Instance variables • Local • Live only within the block that they are created in. When the block ends – so do they. • Remember, variables declared in a method signature are local.
Packages • A package is an encapsulation method used to group classes, interfaces and sub-packages. • Package MUST be the first line in a source file. And become part of the fully qualified name
Naming scheme • The global naming scheme proposed is to use the internet domain name to identify packages. • Edu.heartland.csci130 • Package level access • Access to otherwise unrestricted members • Is the default level
Access Levels • Private • Protected • Default • Public • Other Modifiers • Abstract • Synchronized • Final