Packages, Namespace, InstanceCS2410012011.09.29Cllee@pllab.cs.nthu.edu.tw
What is a Java package?A package is a collection of related classes and interfaces providing namespace managementPackages, which comprise many classes, interfaces, or other packages, support hierarchical organization, and are used to organize large programs into logical and manageable unitsJava packages are namespaces. They allow programmers to create small private areas in which to declare classes. The names of those classes will not collide with identically named classes in different packages
How is a Java package named, created and stored?The names of Java packages should be all lower case and consist of one or more identifiers, separated by dots (periods), such as one.two.threeAll files in a given package must be located in a single subdirectory corresponding to the package name, and the package name will be "translated" to a directory path on the local systemthe part of the path down to one/two/three (or one\two\three) must be part of the classpath for the given system
How is a Java package used?(importv.s. package)You get access to all the classes in a package with an import statement like this:import one.two.three.*; You can also import just one specific class, as inimport one.two.three.SomeClass; When you have access to a class via an import statement, you use its unqualified name like this:SomeClassmyObject = new SomeClass();You can even use a specific class without an import statement, inconvenient and longwinded though that might be, as inone.two.three.SomeClassmyObject = new one.two.three.SomeClass();
Object, Class, InstanceWhat is object?Everything, ex. Human, table, car, etc.What is behavior?Take human as exampleRun, walk, drink, etc.In Java, we use “method” to define the behavior of programWhat is state?Take human as exampleName, color, gender, etc.In Java, we use “variable”(or named field) to define the state of program
Object, Class, Instance(cont.)Example:// 類別(class)叫人類(Human)class Human {//人類所擁有的屬性(property)    String name;    String sex;int height;//人類所擁有的行為(method)voidsetName(String name){}float run(){returnfloat kilometer;    }void eat(String food){}}
Object, Class, Instance(cont.)What is instance(create a instance of an object)?In Java, we want to use Human class. We need to create a instance of an object.Example:classHumanDemo {    Human Taiwanese = new Human();}

Packages namespace instance

  • 1.
  • 2.
    What is aJava package?A package is a collection of related classes and interfaces providing namespace managementPackages, which comprise many classes, interfaces, or other packages, support hierarchical organization, and are used to organize large programs into logical and manageable unitsJava packages are namespaces. They allow programmers to create small private areas in which to declare classes. The names of those classes will not collide with identically named classes in different packages
  • 3.
    How is aJava package named, created and stored?The names of Java packages should be all lower case and consist of one or more identifiers, separated by dots (periods), such as one.two.threeAll files in a given package must be located in a single subdirectory corresponding to the package name, and the package name will be "translated" to a directory path on the local systemthe part of the path down to one/two/three (or one\two\three) must be part of the classpath for the given system
  • 4.
    How is aJava package used?(importv.s. package)You get access to all the classes in a package with an import statement like this:import one.two.three.*; You can also import just one specific class, as inimport one.two.three.SomeClass; When you have access to a class via an import statement, you use its unqualified name like this:SomeClassmyObject = new SomeClass();You can even use a specific class without an import statement, inconvenient and longwinded though that might be, as inone.two.three.SomeClassmyObject = new one.two.three.SomeClass();
  • 5.
    Object, Class, InstanceWhatis object?Everything, ex. Human, table, car, etc.What is behavior?Take human as exampleRun, walk, drink, etc.In Java, we use “method” to define the behavior of programWhat is state?Take human as exampleName, color, gender, etc.In Java, we use “variable”(or named field) to define the state of program
  • 6.
    Object, Class, Instance(cont.)Example://類別(class)叫人類(Human)class Human {//人類所擁有的屬性(property) String name; String sex;int height;//人類所擁有的行為(method)voidsetName(String name){}float run(){returnfloat kilometer; }void eat(String food){}}
  • 7.
    Object, Class, Instance(cont.)Whatis instance(create a instance of an object)?In Java, we want to use Human class. We need to create a instance of an object.Example:classHumanDemo { Human Taiwanese = new Human();}