J2SE basics: 3. Create and use a class namespace and access permission for an object

Source: Internet
Author: User

J2SE basics: 3. Create and use a class namespace and access permission for an object
1: package.

A package is a class container used to save the namespace of a category.
Logically, classes are divided.

Resolves conflicts between classes with the same name.




2: package naming rules:


A: The package must be written in the first line of the source file.

B: All package names must be in lower case.

C: (recommended) package uses the company's domain name.
Www.demo.com
Com. demo. Project name. Module name


3: packages are logically divided by namespaces.


Physically, it is divided by the file system path.

Com. demo. one

After compiling the class, put the compiled class under the com/demo/one folder.

Run
Java com. demo. one. Class Name;



4: Functions of the javac-d option

-D: Specifies the location where class files are stored.

Javac-d./Test_03.java





How to run classes with package and classpath


C:/> java-classpath F: \ aa. bb. cc. dd. ee. Test_03






Package import)
Classes under the same package can call each other.

When classes under different packages need to be used under another package
The path of the class required to import the import keyword.




5. Access to a famous package with no package name:


The classes under the Untitled package can access the classes under the famous package.

Classes under the famous package cannot access the classes under the unknown package.


6. access permission

Private: the access permission in the same class. Other classes cannot be accessed and modified using private.
.

Default: All classes under the same package can be accessed. Different packages cannot be accessed by default
Default class.

Protected: All classes in the same package can be accessed. Different packages cannot be accessed,

Under the same package, the subclass of this class can access the variables and Methods Modified by protected.

Method.


package aa.bb.cc.dd.ee;public class Test_03{public static void main(String args[]){System.out.println("Test_03");}}

package com.testdog;public class Dog{private int dogid;protected String dogName;int getDogid(){return this.dogid;}void setDogid(int dogid){this.dogid = dogid;}protected void setDogName(String dogName){this.dogName = dogName;}}

package com.testprotected;import com.testdog.Dog;public class TestProtected extends Dog{public static void main(String args[]){TestProtected dog = new TestProtected();dog.setDogName("test");System.out.println(dog.dogName);}}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.