java基础知识要点总结之几个重要关键字(关于static、this、final、)
来源:互联网 发布:java静态代理实现 编辑:程序博客网 时间:2024/06/04 23:19
But there are two situations in which this approach is not sufficient. One is if you want to have only one picec of storage for a particular piece of data,regardless of how many objects are created,or even if no objects are created. The other is if you need a method that isn't associated with any particular object of this class.以上是《thinking in java》里关于为什么要使用static关键字的描述,翻译过来即使基于以下两点理由:1.只需要在内存中保存一份数据;2.需要某种不和对象关联的数据。
of course,since static methods don't need any objects to be created before they are used, they cannot directly access non-static members or methods by simply calling those other members without referring to a named object(since non-static members and methods must be tied to a particular object).这句话翻译过来就是说static 方法中不能直接指向非static的属性或者方法。
an important use of static for methods is to allow you to call that method without creating an object.翻译过来就是static 用在方法上的一个重要用法是调用方法的时候不需要创建对象。
this----the reference to the current object,翻译过来就是this是当前对象的引用。
keep in mind that if you're calling a method of your class from within another method of your class , you don't need to use this;you simple call the method .the current this reference is automatically used for the other method.Thus you can say
class Apricot{
void pick(){/**/};
void pit(){
pick();
}
}
Inside pit(), you could say this.pick() but there is no need to.The compiler does it for you automatically. The this keyword is used only for those special case in which you need to explicitly use the reference to the current object. For example, it's often used in return statments when you want to return the reference to the current object:
public class Leaf{
int i = 0;
Leaf increment(){
i++;
return this;
}
void print(){
System.out.println("i = "+i);
}
public static void main(String[] args){
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
}
Beacuse increment() returns the reference to the current object via the this keyword, multiple operations can easily be performed on the same object.
this和static关键词一起来探讨
如果设定一个方法是static,那么这个method没有this。为什么呢?因为static不属于对象的一部分。you can not call non-static methods from inside static methods(although the reverse is possible)。
Final关键字
Final关键字主要是指不可变的。
Final data:
with a primitive, final makes the value a constant, but with an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified.Java does not provide a way to make any arbitrary object a constant.翻译过来就是当final关键字用在基本数据类型的时候,这个数据就是常量,不允许改变;当final关键字用在对象的时候,这个对象的引用不能改变,但是这个这个值本身可以改变。java 没有一种方法保证所有的数值都为常量。数组也当对象看待。指定对象是final后就不能重新进行绑定。
Blank finals
class Poppet{}
class BlankFinal{
final int i = 0; //initialized final
final int j; //blank final
final Poppet p; //blank final reference
//Blank finals MUST be initialized in the constructor
BlankFinal(){
j = 1;//initialize blank final
p = new Poppet();
}
BlankFinal(int x){
j = x; //initialize blank final
p = new Poppet();
}
public static void main(String[] args)
{
BlankFinal bf = new BlankFinal();
}
you're forced to perform assignments to finals either with an expression at the point of definition of the field or in every constructor. This way it's guaranteed that the final field is always initialized before use.
}
Final arguments:Java allows you to make arguments final by declaring them as such in the argument list. This means that inside the method you cannot change what the argument reference points to.But you can still assign a null reference to an argument that's final without the compiler catching it,just like you can with a non-final argument.
- java基础知识要点总结之几个重要关键字(关于static、this、final、)
- java中几个关键字 this static final
- javaSE学习笔记之几个关键字this、super、final、static
- Java中几个关键字 static final this super
- java 几个关键字,final,static
- Java 中关于this、static、final 关键字的使用
- Java关键字final、static、this、super使用总结
- Java关键字final、static、this、super使用总结
- Java关键字final、static、this、super使用总结
- java关键字 this,super,static,final的总结
- JAVA初学之this ,static , super 与final关键字
- Java关键字final、static总结
- JAVA关键字static、final总结
- Java关键字之(static与final)
- JAVA基础--关键字 final/static/this/super
- Java中的this,final,static关键字
- JAVA基础--关键字 final 、static、this、super
- 关于Java中static和final关键字的用法总结
- 清空数据库中某个表的两种操作
- 第三章:Dubbo整合maven+spring+springmvc+mybatis之服务消费搭建
- oc4j(oracle container for j2EE)使用笔记一
- 网上大篇幅的坑人的struts2入门案例
- eclipse配置tomcat,访问http://localhost:8080出现404错误
- java基础知识要点总结之几个重要关键字(关于static、this、final、)
- Java——最长公共子串问题LCS
- 图像处理,从现在开始。
- 微信公众号如何实现宠物店服务微信在线预约
- node eventEmitter
- 科大讯飞面试,一把泪。
- 寻找知识区的盲点
- JSTL核心标签库学习(一)
- Servlet(5)Servlet概念和servlet接口实现(学习Servlet生命周期函数)servlet的实例及其网络拓扑图