• java基础知识要点总结之几个重要关键字(关于static、this、final、)


              you don't get anythin unless you create an object of that class with new, and at that point data storage is created and methods become available.

              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.

      

  • 相关阅读:
    知识点
    Python基础 (下)
    Python基础 (上)
    RESTful和SOAP的区别
    Maven
    centos7下配置的php命令
    centos7下的nginx命令配置
    CentOS 7.4下使用yum安装MySQL5.7.20 最简单的
    word下载
    phpExcel导出表格
  • 原文地址:https://www.cnblogs.com/liqimingaikeke/p/6528160.html
Copyright © 2020-2023  润新知