The 6th chapter of Java Programming thought access rights control __ algorithm

Source: Internet
Author: User
Tags modifiers string format
The 6th chapter of Java Programming thought access rights control

tags (space-delimited): Java learning

The 6th chapter of Java Programming thought Access Control 1 package Library Unit 11 Code Organization 12 Create unique package name 13 Custom ToolPak 2 Java access modifier

6.1 Package: library Unit

When compiling a Java source code file, this file is often referred to as the compilation unit.

Each compilation unit must have a suffix name of. Java, while within the compilation unit you can have a public class that has the same name as the file. 6.1.1 Code Organization

Java uses keyword package to organize specific artifacts into the same group.

If you use the package statement, it must be the first program code in a file other than a comment. Write at the beginning of the file:

Package access;

It means you're declaring that the compilation unit is part of the class library named access. Anyone who wants to use the name must either specify the full name or use the keyword Importin conjunction with access.

For example, suppose the name of the file is Myclass.java, which means that there is only one public class in the file, and the class name must be MyClass.

: Access/mypackage/myclass.java
package access.mypackage;

public class MyClass {
    //...
}

Now, if someone wants to use MyClass or any other public class in Access, there are two ways:

1. Designation of full name

: Access/qualifiedmyclass.java public
class Qualifiedmyclass {public
    static void Main (string[] args) {
        Access.mypackage.MyClass m = 
        new Access.mypackage.MyClasss ();
    }
}

2. Use keyword Import

: Access/importedmyclass.java
import access.package.*;

public class Importedmyclass {public
    static void Main (string[] args) {
        MyClass m = new Myclasss ();
    }
}
6.1.2 Create a unique package name

By convention, the first part of the package name is the reverse-order Internet domain name of the creator of the class, so your package name will be unique and there will be no problem with the name conflict.

The name of the package corresponds to a directory on your machine (for example, package Foo.bar.baz corresponds to Foo/bar/baz). So when the Java program runs and needs to load the. class file, it can determine where the. class file is located on the directory.

The Java interpreter finds the path to the. class file by setting the environment variable CLASSPATH.

/etc/profile
export java_home=/usr/lib/jvm/jdk1.7.0_79  
export classpath=.: $JAVA _home/lib: $JAVA _home/ Jre/lib: $CLASSPATH 
6.1.3 Custom Tools Library
Package net.mindview.util;

Import Java.io.PrintStream;

/**
 * Created by Weijie on 17-5-15.
 *
/public class Print {
    //print with a newline public
    static void Print (Object obj) {
        System.out.printl n (obj);
    }

    Print a newline by itselt public
    static void print () {
        System.out.println ();
    }

    Print with no line break public
    static void Printnb (Object obj) {
        System.out.println (obj);
    }

    The new Java SE5 printf () public
    static PrintStream printf (String format, Object ... args) {return
        System.out . printf (format, args);
    }
}

As you can guess, the location of this file must be in a directory that starts with a classpath position and then Net/mindview. After compiling, you can use the static print () and PRINTNB () method on your system with the import static statement.

Package com.java.chapter6;

Import static net.mindview.util.print.*;

/**
 * Created by Weijie on 17-5-15.
 *
/public class Printtest {public
    static void Main (string[] args) {
        print ("Available from now on!");
        Print (m);
        Print (100L);
        Print (3.14159);
    }

Of course you can also call this:

Package com.java.chapter6;

There is no static keyword and only import to print.
import Net.mindview.util.Print;

/**
 * Created by Weijie on 17-5-15.
 *
/public class Printtest {public
    static void Main (string[] args) {
        //must take the class name Print
        print.print (" Available from now on! ");
        Print.print (m);
        Print.print (100L);
        Print.print (3.14159);
    }
6.2 Java access permission modifiers

Java access modifiers: public,protected and private. If you do not provide any access modifiers, that is, the default access permission, it means "package access".

Public
Key Words Private default protected
The same class Y Y Y Y
Classes in the same package N Y Y Y
Sub Class N N Y Y
Classes in other packages N N N Y

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.