DOC PREVIEW
Penn CIT 591 - Access to Names

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Access to NamesOverviewPart I: NamespacesNames are not uniqueVariable namesDeclarationsDeclare a variable only onceA little puzzleNamespacesThe puzzle solvedAnother little puzzleWhat you should rememberPart II: ScopeScopeMethods may have local variablesCompound statements and blocksBlocks occur in methodsDeclarations in a classDeclarations in a methodNested scopesThe for loopHole in a scopeA common errorSlide 24Part III: Access privilegesPackages = directories = foldersScope and accessHow to access namespublic and private accessPackage and protected accessRead-only accessVocabularyThe EndJan 15, 2019Access to NamesNamespaces,Scopes,Access privileges2OverviewIn Java you name various things: classes, methods, variables, etc.Sometimes you can refer to these things by name, but other times Java gives you an errorYou need to know when you can refer to something by name, and when you can’tYou also need to know how to refer to thingsJava’s rules are complex, but they are not arbitrary--once you understand them, they do make sense!Jan 15, 2019Part I: Namespaces4Hello. I’mMike SmithNames are not uniqueHi. My name isMike SmithMy name isMike Smith5Variable names8int card'Q'char card2int cardHow do we find the card we want?String card"Jack of clubs"refers to6DeclarationsVariables are declared like this: [access] [static] type name [ = value] , ... ;Examples: int m; public double e = 2.718281828459045; static final int ONE = 1, TWO = 2, THREE = 3; public static boolean pluggedIn;Once we declare some variables, where can we use them?Java’s rules are quite complex, but it’s very important to understand them7Declare a variable only once public class Test { public static void main(String args[]) { int var = 5; double var = 8.33; System.out.println(var ); }}var is already defined in main(java.lang.String[])8A little puzzle public class main { int main = 5; public static void main(String args[]) { main main = new main(); System.out.print(main); }}This is a legal program (!); what does it print?Answer: main@ecf76eNext question: why?9NamespacesJava figures out what kind of thing a name refers to, and puts it in one of six different namespaces:package namestype namesfield namesmethod nameslocal variable names (including parameters)labels10The puzzle solved public class main { // type name int main = 5; // field name public static void main(String args[]) { // method name main main = new main(); // local names (incl. args) System.out.print(main); }}Java prints out object main@ecf76e in local variable mainWe haven’t talked about package names or labelsNote that this is te rrible style!11Another little puzzle public class Test { static int five() { return 5; } public static void main(String args[]) { System.out.print(five); }}cannot resolve symbolsymbol :variable five location: class TestAnswer: five() is a method, but five looks like a local variable12What you should rememberA namespace is a place that Java keeps track of namesJava uses six different namespacesIf you name things intelligently, and don’t use the same name for different things, you don’t have to worry much about namespacesJan 15, 2019Part II: Scope14ScopeThe scope of a name is the part of the program in which the name is visibleIn Java, scope rules apply to single methodsVariables declared in a method can only be used within that method; you cannot ever use them anywhere outside the methodBetween classes, we use access rules rather than scope rules15Methods may have local variablesA method may have local (method) variablesFormal parameters are a kind of local variableint add(int m, int n) { int sum = m + n; return sum;}m, n, and sum are all local variablesThe scope of m, n, and sum is the methodThese variables can only be used in the method, nowhere elseThe names can be re-used elsewhere, for other variables16Compound statements and blocksA compound statement consists of zero or more statements inside bracesExamples: { } , { temp = x; x = y; y = temp; }A block consists of zero or more statements or declarations inside bracesExample: { int temp = x; x = y; y = temp; }This distinction is not very important in JavaAnywhere you can use a compound statement, you can use a block17Blocks occur in methodsThe braces in a class declaration do not indicate a block or compound statement: public class MyClass { /* not a block */ }Elsewhere, braces do indicate a block or compound statement: int absoluteValue(int n) { if (n < 0) { return -n; } else return n;}18Declarations in a classThe braces in a class declaration do not indicate a block or compound statement: public class MyClass { // not a block int foo; // instance variable static int bar; // class variableInstance variables and class variables are available throughout the entire class that declares themJava doesn’t care in what order you declare thingsIt's usually good style to put variable declarations first, then constructors, then methods19Declarations in a methodThe scope of formal parameters is the entire methodThe scope of a variable in a block starts where you define it and extends to the end of the block if (x > y) { int larger = x;}else { int larger = y;}return larger;largerscope of largerlargerscope of adifferent largerIllegal: not declared in current scope20Nested scopes int fibonacci(int limit) { int first = 1; int second = 1; while (first < 1000) { System.out.print(first + " "); int next = first + second; first = second; second = next; } System.out.println( ); } limitfirstnextsecond21The for loopThe for loop is a special caseYou can declare variables in the for statementThe scope of those variables is the entire for loopThis is true even if the loop is not a block void multiplicationTable() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) System.out.print(" " + i * j); System.out.println(); } } ji22Scope ofouter mScope ofinner mHole in a scopeConsider the following method:The inner declaration of m hides the outer oneThis is called a hole in the scope of (the outer) mvoid holeInScope( ) { double m = 8.3; System.out.println(m); // prints 8.3 { int m = 5; System.out.println(m); // prints 5 }


View Full Document

Penn CIT 591 - Access to Names

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Access to Names
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Access to Names and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Access to Names 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?