`

Chapter 1 - Declarations and Acess Control

 
阅读更多

1. Identifiers

 

- It must start with a letter, a currency character($), or a connection character such as the underscore(_). It cannot start with a number!.

- After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or number.

- In practice, there is no limit to the number of charcters an identifier can contain.

- You can't use a Java keyword as an identifier.

- Identifiers in Java are case-sensitive.

2. Complete List of Java Keywords(assert added in 1.4, enum added in 1.5)

 

 

abstract boolean break byte case catch
char class  const continue default   do
 double else   extends final   finally  float
 for  goto  if  implements  import  instanceof
 int  interface  long  native  new  package
 private  protected  public  return  short  static
 strictfp  super  switch  synchronized  this  throw
 throws  transient  try  void  volatile  while
 assert  enum

 

Totally 15.

 

3. Source File Declaration Rules

 

- There can be only one pulibc 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 public class.

- 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 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.

 

4. Class Declarations and Modifiers

 

- Access modifiers: public, protected, private.

- Non-access modifiers (including strictfp, final, and abstract).

 

 

Class Access

 

What does it mean to access a class? When we say code from one class (class A) has accessto another class (class B), it means class A can do one of three things:

 

- 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. In effect, access means visibility. If class A can't seeclass B, the access level of the methods and variables within class B won't matter; class A won't have any way to access those methods and variables.

 

Default Access

 

Public Access

 

Other (Nonaccess) Class Modifiers

 

You can modify a class declaration using the keyword final, abstract, or strictfp. These modifiers are in addition to whatever access control is on the class, so you could, for example, declare a class as both public and final. But you can't always mix nonaccess modifiers. You're free to use strictfp in combination with final, for example, but you must never, ever, ever mark a class as both final and abstract. 

 

 

strictfp is a keyword and can be used to modify a class or a method, but never a variable. Marking a class as strictfp means that any method code in the class will conform to the IEEE 754 standard rules for floating points. Without that modifier, floating points used in the methods might behave in a platform-dependent way. If you don't declare a class as strictfp, you can still get strictfp behavior on a method-by-method basis, by declaring a method as strictfp.

 

Final Classes

 

Abstract Classes

 

5. Declare Interfaces

 

Declaring an Interface

 

- All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract.

- All variables defined in an interface must be public, static, and final—in other words, interfaces can declare only constants, not instance variables.

- Interface methods must not be static.

- Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.)

- An interface can extend one or more other interfaces.

- An interface cannot extend anything but another interface.

- An interface cannot implement another interface or class.

- An interface must be declared with the keyword interface.

- Interface types can be used polymorphically (see Chapter 2 for more details).

 

 

The following is a legal interface declaration:

public abstract interface Rollable { }

 

Declaring Interface Constants

 

 

You need to remember one key rule for interface constants. They must always be

public static final

 

public int x = 1; // Looks non-static and non-final,

                         // but isn't!

int x = 1; // Looks default, non-final,

               // non-static, but isn't!

static int x = 1; // Doesn't show final or public

final int x = 1; // Doesn't show static or public

public static int x = 1; // Doesn't show final

public final int x = 1; // Doesn't show static

static final int x = 1 // Doesn't show public

public static final int x = 1; // what you get implicitly

 

 

 

 

分享到:
评论

相关推荐

    OCA OCP Java SE 7 Programmer I - II Study Guide(SYBEX,2014)

    * Declarations and access control * Object orientation * Assignments * Operators * Strings and arrays * Flow control and exceptions * Assertions and Java 7 exceptions * String processing, data ...

    The C programming Language(chm格式完整版)

    Chapter 1: A Tutorial Introduction Getting Started Variables and Arithmetic Expressions The for statement Symbolic Constants Character Input and Output File Copying Character Counting Line ...

    OCP Java SE 8 Programmer II Exam Guide (Exam 1Z0-809)

    Declarations, access control, and enums Object orientation Assertions and exceptions Dates, times, locales, and resource bundles I/O and NIO Generics and collections Inner classes Lambda expressions ...

    The Art of Assembly Language Programming

    Memory Layout and Access 4.0 - Chapter Overview 4.1 - The 80x86 CPUs:A Programmer's View 4.1.1 - 8086 General Purpose Registers 4.1.2 - 8086 Segment Registers 4.1.3 - 8086 Special ...

    C# 5.0 Programmer’s Reference

    Beginning through intermediate-level programmers will benefit from the accessible style of C# 5.0 Programmer’s Reference and will have access to its comprehensive range of more advanced topics....

    The C programming Language

    Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels <br>Chapter 4: Functions and ...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    Chapter 10Control Flowdescribes how control statements direct the order of statement execution. <br>Chapter 11Generic Typesdescribes generic types: how they are written and used, their power, and ...

    Turbo C++ 3.0[DISK]

    instant access to the Borland forums with their libraries of technical information and answers to common questions. If you are not a member of CompuServe, see the enclosed special offer, and ...

    Turbo C++ 3.00[DISK]

    instant access to the Borland forums with their libraries of technical information and answers to common questions. If you are not a member of CompuServe, see the enclosed special offer, and ...

    ebook-Practical C Programming, 3rd Edition(ebook).pdf

    1. Multiply and divide come before add and subtract. 2. Put parentheses around everything else. Consider two programs. One was written by a clever programmer using all the tricks. The program ...

    The C Programming Language 第二版 英文版

    Chapter 1 - A Tutorial Introduction.............................. 1.1 Getting Started................................ 1.2 Variables and Arithmetic Expressions........... 1.3 The for statement.........

Global site tag (gtag.js) - Google Analytics