Docsity

Organization of a Java Program - Comparative Languages | CS 480, Study notes of Linguistics

Toggle sidebar
bg1
Packages
cs480 (Prasad) L8Packages 1
Partitioning class name space
Access control of names
Organization of a Java Program
• Java Program
–Forestof packages
• E.g., $PWD, java, javax, etc.
• Package (Tree)
– Directory of sub-packages and compilation units
cs480 (Prasad) L8Packages 2
• E.g., java.io, java.util, java.lang.Thread, java.applet.App let;
• Compilation Unit (Leaf)
– File containing class and interface definitions with at
most one public class
• CLASSPATH environment variable
– Set to full-path names of roots of trees in the forest
CLASSPATH
cs480 (Prasad) L8Packages 3
= (Sub-)Package
= Compilation Unit
package statement
– Omitted : anonymous package
•For *.class byte code files to be accessible to
the JVM, ensure “.” ($PWD) is contained in
the environment variable CLASSPATH.
package Pk;
cs480 (Prasad) L8Packages 4
–
package Pk;
• If a class is declared inside package Pk (or
subpackage Pk.Sk), then the *.java source
files must be stored in the directory Pk (or
Pk/Sk), and the CLASSPATH must contain
at least .../Pk.
pf3
pf4

Partial preview of the text

Download Organization of a Java Program - Comparative Languages | CS 480 and more Study notes Linguistics in PDF only on Docsity!

Packages

cs480 (Prasad) L8Packages 1

Partitioning class name space

Access control of names

Organization of a Java Program

• Java Program

  • Forest of packages
    • E.g., $PWD, java, javax, etc.

• Package (Tree)

  • Directory of sub-packages and compilation units

cs480 (Prasad) L8Packages 2

  • E.g., java.io, java.util, java.lang.Thread, java.applet.Applet;

• Compilation Unit (Leaf)

  • File containing class and interface definitions with at

most one public class

• CLASSPATH environment variable

  • Set to full-path names of roots of trees in the forest

CLASSPATH

= (Sub-)Package

= Compilation Unit

package statement

  • Omitted : anonymous package
    • For * .class byte code files to be accessible to

the JVM, ensure “. ” ($PWD) is contained in

the environment variable CLASSPATH.

package Pk;

  • package Pk;
    • If a class is declared inside package Pk (or

subpackage Pk.Sk), then the *.java source

files must be stored in the directory Pk (or

Pk/Sk ), and the CLASSPATH must contain

at least .../Pk.

import statement ( for programmer convenience )

– importing a type explicitly

  • import java.util.StringTokenizer;

– importing type-on-demand

  • import java.io.*;

Access Control

cs480 (Prasad) L8Packages 5

– private : inaccessible from outside the class

– default : accessible in the package

– protected : accessible in the subclasses

and the package

– public : no restriction on accessibility

private default protected^ public

same class (^) Y Y Y Y

same package

subclass N^ Y^ Y^ Y

cs480 (Prasad) L8Packages 6

same package

non-subclass N^ Y^ Y^ Y

different package

subclass N^ N^ Y^ Y

different package

non-subclass N^ N^ N^ Y

class S extends C { public int i; protected int j; private int k; int p(C c) { return c.x ; // + c.y + c.z ; }

class C { public int x; protected int y; private int z; int p(C c) { return c.x + c.y + c.z ; }

Access Control : C and S in different packages

int q(S s) { return s.x + s.y + // s.z + s.i + s.j + s.k ; }}

int q(S s) { return s.x + s.y +((C) s).y +((C) s).z // + s.z

  • s.i ; // + s.j + s.k ; }}

Notes

  • Public, protected and private fields of a direct instance

(e.g., c.x, c.y, c.z) of a class are accessible in the

class (C’s text), while the private fields of a subclass

instance (e.g., s.z) are accessible in the class (C’s text)

only via a static cast (e.g., (C) s.z).

  • The inherited protected field due to parent class (C) of

a direct instance of a subclass (e.g., s.y) is available in

subclass (S’s text).

  • Note that the protected field of a direct class instance (e.g., c.y) is not available in the subclass (S’s text) and the protected field of a subclass instance is not available in the parent class (C’s text) (e.g., s.j).

Scope and Lifetime

  • The scope of an identifier declaration is

the region of text in which the declaration is effective.

  • E.g., class scope, block scope, global, etc.
  • The lifetime of a variable (or an object)

bound to an identifier is the period during which it exists in the memory.

  • E.g., Static variables and objects allocated via new

on heapheap have “infinite” lifetime. (garbage collection)

  • E.g., Formal parameters and local variables of a

method are allocated on stack.stack. (LIFO discipline)

Enumeration myEnumerate(final Object[] arr) {

class E implements Enumeration { int count = 0; public boolean hasMoreElements() { return count < arr.length; } public Object nextElement() { return arr[count++]; } } return new E(); }

  • Scope of arr is the body of myEnumerate function.
  • Lifetime of the array instance referenced by arr is

“infinite”. It survives as long as the Enumeration

object returned is “alive”.