Java的Variable Types变量类型-笔记

来源:互联网 发布:老徐杂货铺淘宝号 编辑:程序博客网 时间:2024/06/06 21:41

Java Variable Types 变量类型

本文参考这里

data-type variable [ = value][, variable [= value] ...];

Local 变量

  • declared in methods, constructors, or blocks
  • 不能使用访问限定符(Access modifiers cannot be used for local variables)
  • Local变量放在 stack 里,若没初始化则无默认值

Instance 变量

  • declared in a class, but outside a method, constructor or any block.
  • 实例变量在object被new时创建,在object被destroyed的时候销毁
  • 可以使用访问限定符(Access modifiers can be given for instance variables.)
  • 在class内,实例变量具有可见性(visible for all methods, constructors and block in the class)
  • 实例变量有默认值(Instance variables have default values)

Class/static 变量

  • ClassName.VariableName 访问
  • 一个类不管创建多少objects,static变量仅有一个拷贝值
  • static 变量存储在static区(Static variables are stored in static memory)
  • static变量在程序开始时创建,程序停止时销毁
  • 常见地,static 变量多被声明为 public
  • 常见地,static 变量多同时用在 final 变量上
  • 默认值参照实例变量的

例:

public static final String DEPARTMENT = "Development";

0 0
原创粉丝点击