Declare Classes

Source File Declaration Rules
       ■ There can be only one public class per source code file.
       ■ Comments can appear at the beginning or end of any line in the source code
  file; they are independent of any of the positioning rules discussed here.
       ■ If there is a public class in a file, the name of the file must match the name
  of the public class. For example, a class declared as public class Dog { }
  must be in a source code file named Dog.java.
       ■ If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
       ■ If there are import statements, they must go between the package statement
  (if there is one) and the class declaration. If there isn't a package statement,
  then the import statement(s) must be the first line(s) in the source code file.
  If there are no package or import statements, the class declaration must be
  the first line in the source code file.
       ■ import and package statements apply to all classes within a source code file.
  In other words, there's no way to declare multiple classes in a file and have
  them in different packages, or use different imports.
       ■ A file can have more than one nonpublic class.
       ■ Files with no public classes can have a name that does not match any of the
  classes in the file.

Class Modifiers fall into two categories:
       ■ Access modifiers: public, protected, private, default.
       ■ Non-access modifiers (including strictfp, final, and abstract).
Class Access
       ■ Create an instance of class B.
       ■ Extend class B (in other words, become a subclass of class B).
       ■ Access certain methods and variables within class B, depending on the access control of those methods and variables.

Default Access
       A class with default access has no modifier preceding it in the
declaration! It's the access control you get when you don't type a modifier in the
class declaration. Think of default access as package-level access, because a class with default access can be seen only by classes within the same package.

Public Access        
       A class declaration with the public keyword gives all classes from all packages access to the public class. In other words, all classes in the Java Universe (JU) have access to a public class. Don't forget, though, that if a public class you're trying to use is in a different package from the class you're writing, you'llstill need to import the public class.

Other (Nonaccess) Class Modifiers

Final Classes       
When used in a class declaration, the final keyword means the class can't be subclassed. In other words, no other class can ever extend (inherit
from) a final class, and any attempts to do so will give you a compiler error.

Abstract Classes          
An abstract class can never be instantiated. Its sole purpose, mission in life, raison d'être, is to be extended (subclassed). (Note, how-ever, that you can compile and execute an abstract class, as long as you don't try to make an instance of it.)





请使用浏览器的分享功能分享到微信等