the Java keywords: final ,static &public

10. the Java keywords: final &static&public

10.1 the keyword "final"

​ only for classes, methods and variables:

  • for classes: the classes cannot be inherited.

  • for methods: the methods cannot be overwrite.

  • for variable: the variable can be assigned only once. And after then the variable will be treated as a constant , be stocked at the static area(constant area)

    Note: if a pointer is modified by “final”, it’s address cannot be changed. but the attributes of object it point can be assigned.

10.2 the keyword “static

  • only for methods and variables, cannot be used on classes.

  • The static methods or variables will be shared by all of the class’s objects.

  • The static methods or variables are only related to the class, not the object. then all the object don’t need to apply heap’s memory for these attributes.

  • when to use static? all the objects are share a same attributes or method.

    • for common function extraction from class. eg:
    public static boolean isNull(sting [] args){// judge a string Null or not
        if(args!=Null){
            return ture;
        }else
        return false;    
    }
    
  • the static method and variable are stocked in the “Method Area”, not in heap. And if the class is public , other class’s objects can access it,too. So it can be used directly by "class.method&variable or interface.method " , do not need to new an object in heap, saving the memory.

Note: Static method can only access the static contexts, cannot access the non-static contexts. The non-static contexts must be instantiated to a object, then it can be accessed by static method

Keyword package: 指定类的路径。同一个包表示路径相同。

​ 作用:1、区分同名的类

​ 2、 通过包名(路径)找到对应的类

10.3 Privilege modifier:

public>protected>(default)>private.

可以应用修饰符的对象有三种:类(Class)、方法(Method)、变量(Variable)

修饰类成员:能访问?1 :0publicprotecteddefaultprivate
同一个类中:1111
同一个包中:1110
不同包,子类中:1100
不同包,非子类中:1000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值