Chapter 1 - Declarations and Acess Control

1 篇文章 0 订阅

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)

 

 

abstractbooleanbreakbytecasecatch
charclass constcontinuedefault  do
 doubleelse  extendsfinal  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

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值