Java修饰符/关键字
修饰符分类:
- 权限修饰符:public、protected、default、private
- 其他修饰符:abstract、static、final、transient、volatile、native、synchronized、strictfp
public:
- public的使用对象:public可以修饰 类、抽象类、接口,还可以修饰 方法和变量
- public修饰的对象可以被所以其他类访问
protected:
- protected的使用对象:protected可以修饰 方法和变量,不能修饰类(外部类) ,内部类较特殊,后面单独研究。
- protected修饰的对象可以被 同一包内的类或它的子类所访问
default:
- default的使用对象:default可以修饰 类、抽象类、接口,还可以修饰 方法和变量
- default修饰的对象可以被 同一包下的类访问
private:
- private的使用对象:private可以修饰 方法和变量,不能修饰类(外部类)
- private修饰的对象只在该类内部可见,外部不可访问,包括他的子类也不能访问、不能覆盖
换个角度看:
- 类、抽象类、接口可以被public、default修饰,default修饰的类、抽象类、接口只本包内可见
- 方法和变量可以被public、protected、default、private所有权限修饰符修饰
Q&A:
- Q:为什么类不能被private、protected修饰?
- A:private好理解,private的类完全孤立不能为外界所访问,毫无用处;就类而言如果被protected修饰时等价于default,其他的包的类根本无法引用它而产生子类
abstract:
- abstract用于修饰 类、接口或方法
- abstract修饰的类即抽象类不能初始化,含未实现即abstract的方法
- 所有的接口默认是abstract的,当然也可以显示用abstract修饰
final:
- final用于修饰 类、变量或方法
- final类不能被继承、final变量不能被修改、final方法不能被覆盖
- final变量必须初始化,可以在声明时或构造函数里面赋值
- final变量如果是引用变量,变量的value可变,但不能给引用重新赋值(新对象)
- final变量如果同时被static修饰,那必须在声明时初始化,不能放到构造函数
- 构造函数里面调用的方法最好是final的
- Oracle Tutorial: http://docs.oracle.com/javase/tutorial/java/IandI/final.html
class FinalTest{
private final List foo = new ArrayList();// public FinalTest()
{
//foo = new ArrayList(); 如果声明时没有初始化,可以在这里初始化
} public void updateFoo(){
foo.add( new String() );//可以改变被应用对象的值
//foo = new ArrayList(); //编译错误,不能赋予新的对象
}
}
static:
- static可修饰 变量、方法、代码块、内部类,不能修饰外部类
- static的变量或方法属于具体的类,而不是某个实例,所有实例共享该变量或方法,实例可以改变static变量,也可以通过类名直接调用
- static的方法不能直接访问 实例变量或方法,static方法里面也不能使用this
transient:
- 理解transient之前需要先理解序列号serialization
- What is serialization?
- 序列化是将对象状态持久化的一个过程,这里的持久化即将一个对象被转化成stream of bytes并存到文件中。反之,我可以从bytes里面deserialization一个对象。要实现这个功能类或接口必须继承Serialization接口
- Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes and stored in a file. In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the
Serialization
interface. It is a marker interface without any methods.
- Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes and stored in a file. In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the
- 序列化是将对象状态持久化的一个过程,这里的持久化即将一个对象被转化成stream of bytes并存到文件中。反之,我可以从bytes里面deserialization一个对象。要实现这个功能类或接口必须继承Serialization接口
- What is the transient keyword and it’s purpose?
- 默认情况下,序列化会保存对象的所有变量,如果你不想对象被持久化,你可以把它声明为transient。
- By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as
transient
. If the variable is declared astransient
, then it will not be persisted. That is the main purpose of thetransient
keyword.
- By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as
- 默认情况下,序列化会保存对象的所有变量,如果你不想对象被持久化,你可以把它声明为transient。
- Example
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; class NameStore implements Serializable {
private String firstName;
private transient String middleName;
private String lastName; public NameStore (String fName, String mName, String lName) {
this.firstName = fName;
this.middleName = mName;
this.lastName = lName;
} public void func(){
System.out.println("I'm func");
} public String toString() {
StringBuffer sb = new StringBuffer(40);
sb.append("First Name : ");
sb.append(this.firstName);
sb.append("Middle Name : ");
sb.append(this.middleName);
sb.append("Last Name : ");
sb.append(this.lastName);
return sb.toString();
}
} public class TransientExample {
public static void main(String args[]) throws Exception {
NameStore nameStore = new NameStore("Steve", "Middle", "Jobs");
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
"nameStore"));
// writing to object
o.writeObject(nameStore);
o.close(); // reading from object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"nameStore"));
NameStore nameStore1 = (NameStore) in.readObject();
nameStore1.func();
System.out.println(nameStore1);
}
/**Output
*
*I'm func
*First Name : SteveMiddle Name : nullLast Name : Jobs
*
***/
}
native:
- 标识一个方法将会用其他语言实现而不是Java,It works together with JNI(Java Native Interface)
- Native方法曾经主要用于写一些对性能要求很高的代码块,随之Java变得越来越快,Native方法变得不常用了,Native method is currently needed when
- 调用其他语言写的library
- 你需要访问那些Java不可及的系统资源,只能Native方法调用其他语言实现
- Native的方法就像Abstract的方法一样没有方法体。
strictfp:
- Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.
- From the JLS:
- Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats. Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.
- In other words, it's about making sure that Write-Once-Run-Anywhere actually means Write-Once-Get-Equally-Wrong-Results-Everywhere.
- With strictfp your results are portable, without it they are more likely to be accurate.
volatile:
http://stackoverflow.com/questions/106591/do-you-ever-use-the-volatile-keyword-in-java
synchronized:
Java修饰符/关键字的更多相关文章
- Java修饰符关键字的顺序
Java语言规范建议按以下顺序列出修饰符: 1. Annotations 2. public 3. protected 4. private 5. abstract 6. static 7. fina ...
- java的基础语法(标识符 修饰符 关键字)
Java 基础语法 一个 Java 程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如 ...
- JAVA修饰符类型(public,protected,private,friendly)
转自:http://www.cnblogs.com/webapplee/p/3771708.html JAVA修饰符类型(public,protected,private,friendly) publ ...
- Java修饰符关键词大全
所以我以此主题写了这篇文章.这也是一个可用于测试你的计算机科学知识的面试问题. Java修饰符是你添加到变量.类和方法以改变其含义的关键词.它们可分为两组: 访问控制修饰符 非访问修饰符 让我们先来看 ...
- JAVA修饰符类型(转帖)
JAVA修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的任何类均可以访问:protected的类.类属变量及方法,包内的 ...
- java修饰符public final static abstract transient
JAVA 修饰符public final static abstract transient 关键字: public final static abstract ... 1.public prot ...
- Java基础之Java 修饰符
前言:Java内功心法之Java 修饰符,看完这篇你向Java大神的路上又迈出了一步(有什么问题或者需要资料可以联系我的扣扣:734999078) Java语言提供了很多修饰符,主要分为以下两类: 访 ...
- 浅析java修饰符之public default protected private static final abstract
浅析java修饰符之public default protected private static final abstract 一 修饰符的作用:用来定义类.方法或者变量,通常放在语句的最前端 ...
- 【java初探外篇01】——关于Java修饰符
本文记录在学习Java语言过程中,对碰到的修饰符的一些疑问,在这里具体的拿出来详细学习和记录一下,以作后续参考和学习. Java修饰符 Java语言提供了很多修饰符,但主要分两类: 访问修饰符 非访问 ...
随机推荐
- 都是stm32的JTAG引脚惹的祸
转载请注明出处:http://blog.csdn.net/qq_26093511/article/category/6094215 最近在调试08接口的LED显示屏,使用的是自己做的STM32板子. ...
- python超大数计算
In [26]: %time a = 6789**100000CPU times: user 0 ns, sys: 0 ns, total: 0 nsWall time: 6.2 µsIn [27]: ...
- shell入门-sed-2替换功能
sed的替换功能和vim语法挺像的 把1到10行的nologin替换成login [root@wangshaojun ~]# sed '1,10s/nologin/login/g' 1.txt roo ...
- RubyGems 镜像 - 淘宝网
为什么有这个? 由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败.所以你会与遇到 gem install rack 或 bundle ...
- C#调用ODBC连接SQL Server数据库的存储过程
OdbcConnection con = new OdbcConnection("Driver={SQL Server};server=PC-200201070359;uid=sa;pwd= ...
- JVM原理解析
JVM主要的功能: 内存分配 程序调度 内存释放(栈等自动释放.堆垃圾回收) 异常处理 https://www.cnblogs.com/dingyingsi/p/3760447.html https: ...
- HTML5-A*寻路算法2
设置起点 设置终点 设置障碍 清除障碍 允许斜向跨越
- 《精通Spring4.X企业应用开发实战》读后感第六章(国际化)
- hdu 2897 邂逅明下 (简单巴什博弈)
题目链接 邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)
Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...