标签:ide 静态类 赋值 构造器 初始化块 修改 实例 return ini
一、关键字final
注意:变量用static final修饰:全局常量
二、关键字static
static,静态的,可以用来属性、方法、*代码块(或初始化块)、*内部类
static修饰属性(类变量):
static修饰方法(类方法):
注意:静态的结构(static的属性、方法、代码块、内部类)的生命周期要早于非静态的结构,同时被回收也要晚于非静态的结构
静态的方法内是不可以有this或super关键字的!
static测试实例:
package com.yyx.pratice; public class JavaPratice { public static void main(String[] args) { Account a1 = new Account("abc123", 1000); Account a2 = new Account("abc456", 2000); Account a3 = new Account("abc456", 2000); System.out.println(a1); System.out.println(a2); System.out.println(a3); } } class Account { private int id;// 账号 private String password;// 密码 private double balance;// 余额 private static double rate = 0.05;// 利率 private static double minBalance = 1;// 最小余额 private static int init = 1000; public Account(String password, double balance) { this.id = init++; this.password = password; this.balance = balance; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public static double getRate() { return rate; } public static void setRate(double rate) { Account.rate = rate; } public static double getMinBalance() { return minBalance; } public static void setMinBalance(double minBalance) { Account.minBalance = minBalance; } @Override public String toString() { return "Account [id=" + id + ", password=" + password + ", balance=" + balance + "]"; } }
标签:ide 静态类 赋值 构造器 初始化块 修改 实例 return ini
原文地址:https://www.cnblogs.com/xianya/p/9125982.html