28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Road Map Introduction to object oriented programming. Classes
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Access to Names Namespaces, Scopes, Access privileges.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How.
16-Jul-15 Access. 2 Overview Questions covered in this talk: How do we access fields and methods? Why have access restrictions? What can have access restrictions?
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
UML Basics & Access Modifier
Writing Classes (Chapter 4)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
10-Nov-15 Java Object Oriented Programming What is it?
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programing ANB/PSC 290 Jeff Schank.
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Yanal Alahmad Java Workshop Yanal Alahmad
Namespaces, Scopes, Access privileges
Class Structure 16-Nov-18.
Lesson A4 – Object Behavior
Class Structure 28-Nov-18.
Polymorphism 28-Nov-18.
Group Status Project Status.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Recap Week 2 and 3.
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Classes, Objects and Methods
A Methodical Approach to Methods
Classes and Methods 15-Aug-19.
CSG2H3 Object Oriented Programming
Presentation transcript:

28-Jun-15 Access to Names Namespaces, Scopes, Access privileges

2 Overview 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!

28-Jun-15 Part I: Namespaces

4 Hello. I’m Mike Smith Names are not unique Hi. My name is Mike Smith My name is Mike Smith

5 Variable names 8 int card 'Q' char card 2 int card How do we find the card we want? String card "Jack of clubs" refers to

6 Declarations Variables are declared like this: [access] [ static ] type name [ = value],... ; Examples: int m; public double e = ; 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 them

7 Declare 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[])

8 A 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: Next question: why?

9 Namespaces 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) labels

10 The 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 in local variable main We haven’t talked about package names or labels Note that this is terrible style!

11 Another little puzzle public class Test { static int five() { return 5; } public static void main(String args[]) { System.out.print(five); } } cannot resolve symbol symbol :variable five location: class Test Answer: five() is a method, but five looks like a local variable

12 What 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 namespaces

28-Jun-15 Part II: Scope

14 Scope 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 rules

15 Methods 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 variables

16 Compound 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 block

17 Blocks 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; }

18 Declarations 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 methods

19 Declarations 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; larger scope of larger larger scope of a different larger Illegal: not declared in current scope

20 Nested 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( ); } limit first next second

21 The 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(); } } j i

22 Duplicate definitions Consider the following method:

23 Duplicate definitions void holeInScope( ) { double m = 8.3; System.out.println(m); { int m = 5; // illegal! System.out.println(m); } } void holeInScope( ) { { double m = 8.3; System.out.println(m); } { int m = 5; // legal System.out.println(m); } }

24 A common error class Something { String example; public static void main(String[] args) { String example = "xyz"; testPrint(); } static void testPrint() { System.out.println(example); } } Output: null Why? Local variables shadow class variables with the same name The problem is harder to notice in a longer program

25 What you should remember Names (of variables, constructors, or methods) declared anywhere in a class are available everywhere within the class (order doesn’t matter) Formal parameters of a method are available everywhere within the method Variables declared in a block are available from where they are declared to the end of that block Variables declared in a for loop are available within the for loop

28-Jun-15 Part III: Access privileges

27 Packages = directories = folders A public class must be put in a file of the same name Example: public class Test {... } must be saved in a file named Test.java Similarly, if you use a package statement, the file must be in a directory (folder) of the same name Example: If you specify package assignment42; then it must be in a directory named assignment42 Only large programs should be put in multiple directories We aren’t writing large programs in this course

28 Scope and access Local variables (formal parameters and method variables) are available only within the method that declares them, never anywhere else Names (of variables, constructors, and methods) declared in a class are available everywhere within that class, and may be available inside other classes Access to these names is controlled by the access modifiers public, package (default), protected, and private

29 How to access names From outside class Person: you can access an instance variable (say, of jack ) by: jack.age you can access a class variable by: Person.population As a (confusing) convenience, you can also access a class variable by way of any instance of that class: jack.population // works, but is confusing--avoid These techniques also work for methods and constructors

30 public and private access If you declare a name to be public, you are allowing every other class in the world to see it and to change it (called read- write access) If random changes to this name can invalidate the object, it should not be public If you declare a name to be private, you are saying that only the class in which it is declared can see it and change it If all your.java files are in the same directory (recommended for this course), there is no difference between public, protected, and package

31 Package and protected access Package access means that a name is available everywhere in the same package (the same directory) protected access means that a name is available everywhere in the same package (the same directory), but also to any subclasses, wherever they may be protected access is “more public” than package access

32 Read-only access If you want a variable to be read-only: Declare the variable to be private Provide a “getter” method to return its value Do not provide a “setter” method to set its value Example: public class Person { private int population; int getPopulation( ) { return population; }... }

33 Vocabulary namespace -- a place that Java keeps track of names scope of a name -- the part of the program in which the name is visible compound statement -- zero or more statements inside braces block -- zero or more statements or declarations inside braces access modifier -- one of the keywords public, protected, and private

34 The End