类变量和类方法

类变量与类方法

package Static_;

public class VisitStatic {
    public static void main(String[] args) {
        //类变量是随着类的创建微软创建,即没有创建对象实例也可以访问
        System.out.println(A.name);
        A a = new A();
        //通过创建对象名.类变量名也可以访问
        System.out.println(a.name);
    }
}
class A{
    //类变量  
    //类变量的访问也必须遵守相关的访问权限
    public static String name="张三";
}
类变量使用细节:

1、什么时候需要使用类变量

当我们需要某个嘞的所有对象都共享一个变量时,考虑使用类变量(静态变量)

2、类变量和实例变量(普通属性)的区别

类变量是所有对象共享的,而实例变量是每个对象独享的

3、类变量使用static修饰的,没有static修饰的就是实例变量/非静态变量

4、类变量可以通过类名.类变量名 或 对象名.类变量名访问 (前提:类变量的访问满足访问修饰符的访问权限和范围)

5、实例变量(普通属性)不能通过类名.类变量名访问(没有创建对象则访问不了类的属性)

public class VisitStatic {
    public static void main(String[] args) {
        System.out.println(B.n1);//不能访问
        System.out.println(B.n2);//可以访问
    }
}class B{
    public int n1=100;
    public static int n2=200;
}

6、类变量是在类加载时就初始化了,即没有创建对象,只要类加载了也就可以使用类变量了

package Static_;

public class VisitStatic {
    public static void main(String[] args) {
        //静态变量是类加载的时候就创建了,所以我们没有创建对象实例也可以通过类名.类变量名来访问
        System.out.println(C.s1);
    }
}
class C{
    public static String s1="hjsja";
}

7、类变量的生命周期随着类的加载开始,随着类的消亡销毁

类方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yl5quC4a-1670096991211)(C:\Users\15052\AppData\Roaming\Typora\typora-user-images\image-20221204003838240.png)]

package Static_;

public class StatcMe {
    public static void main(String[] args) {
        stu stu1 = new stu("marr");
        stu1.payFee(1000);
        stu stu2 = new stu("tom");
        stu2.payFee(2000);

        stu.showFee();//类名.静态方法名
        stu1.showFee();//对象名.静态方法名
        

    }
}
class stu{
    private String name;
    private static double fee=0;

    public stu(String name) {
        this.name = name;
    }
    //当方法使用static修饰之后,该方法就是静态方法
    //静态方法就可以访问静态变量
    public static void payFee(double fee){
        stu.fee+=fee;//累加
    }
//    public void payFee1{
//        stu.fee;
//    }
    public static void showFee(){
        System.out.println("总学费有:"+stu.fee);
    }
}

类方法使用注意事项和细节

1、类方法和普通方法都是随着类的加载而加载,将结构信息存储在方法区,

类方法中没有this的参数,

普通方法中隐含this的参数

2、类方法可以通过类名调用,也可以通过对象名调用

3、普通方法和对象有关,需要通过对象名调用,不能通过类名调用

4、类方法中不允许使用和对象有关的关键字,比如this和super,普通方法可以

class D{
    private int n1=100;
    public void say(){
        //非静态方法
        System.out.println(this.n1);
    }
    public static void hi(){
        //静态方法
        //类方法中不允许使用和对象有关的关键字,如this  super
        System.out.println(this.n1);//报错  无法从 static 上下文引用 'Static_.D.this'
    }
    
}

5、类方法中只能访问静态成员—>成员(变量 方法)

6、普通方法中既可以访问静态成员也可以访问普通成员

class D{
    private int n1=100;
    private static int n2=300;
    public void say(){
        //非静态方法
        System.out.println(this.n1);//普通变量
        System.out.println(D.n2);//静态变量
    }
    public static void hi(){
        //静态方法
        //类方法中不允许使用和对象有关的关键字,如this  super
        System.out.println(this.n1);//报错  无法从 static 上下文引用 'Static_.D.this'
        System.out.println(n1);//报错   无法从 static 上下文引用非 static 字段 'n1'
        System.out.println(n2);//静态变量
    }

}

注意:普通方法也叫非静态方法,类方法也叫静态方法,区别在于是否使用static修饰

stem.out.println(n1);//报错 无法从 static 上下文引用非 static 字段 ‘n1’
System.out.println(n2);//静态变量
}

}


注意:普通方法也叫非静态方法,类方法也叫静态方法,区别在于是否使用static修饰



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值