java 变量和常量

时间:2020-09-17 12:02:54   收藏:0   阅读:26

java 中变量作用域

变量分为:

类变量;定义在类中,有关键字static,从属于类,与类一起产生一起消失。

实例变量;定义在类中,可以不初始化,自动初始化为默认值,boolean默认为false

局部变量;定义在方法中,必须声明和初始化

public class Variable{
    static int allClicks = 0;//类变量
    static double salary = 2500;
    String str = "hello world";//实例变量
    String name = "田野";
    int age = 22;
    public static void main(String[] args){
        Variable var = new Variable();
        System.out.println(var.age);
        System.out.println(var.name);
    }
    public void method(){
        int o = 1;// 局部变量
        System.out.println(salary);
    }
}

java 中常量 关键字final

final 常量名 = 值; final为修饰符,不区分先后先后顺序。

eg: static final double PI = 3.1415;

eg: final static double PI = 3.1415;

常量名一般使用大写字符 + 下划线。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!