8
CSCI 224 Scope – Package – Access

CSCI 224

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI 224

CSCI 224

Scope – Package – Access

Page 2: CSCI 224

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.

Page 3: CSCI 224

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.

Page 4: CSCI 224

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)

Page 5: CSCI 224

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.

Page 6: CSCI 224

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

Page 7: CSCI 224

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

Page 8: CSCI 224

Access Levels

Private Protected Default Public Other Modifiers

Abstract Synchronized Final