杂论-final-static-final static

8 篇文章 0 订阅

直接上代码


public class SIx {
public int a = 10;//属于对象
public final int b = 20;//final修饰 属于对象,编译期间确定
public static int c = 30;//static 属于方法区
public static final int  d = 40;//方法区


public String str = "one";//属于对象,堆  one在常量池
public final String str2 = "two";//属于对象 two在常量池
public static String str3 = "three";//方法区  three常量池
public static final String  str4 = "four";//方法区 four常量池

public static void main(String[] args) {
    SIx s = new SIx();              //s在栈,SIX在堆
    String str1 = "hehehaha";       //str1在栈,"hehehaha"属于常量池
    String str2 = "hehe";           //str2在栈,"hehe"属于常量池
    String str3 = str2 + "haha";    //str3在栈 ,"haha"属于常量池

    final String str4 = "hehe";     //str3在栈 ,"hehe"属于常量池



}

}

由上面代码可知
final修饰的变量对变量的存储区域是没有任何影响的,只是将其作为了常量存储,不可改变,被修饰的类不可被继承。
被static修饰的变量都存储于方法区,其被修饰的变量和对象,在对象未创建之前就可以被调用,被static修饰的方法(变量)都为静态方法(变量),jvm虚拟机只会加载一次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值