1.static静态变量1.静态变量被同一个类的所有对象共享
2.static类变量在类加载的时候就生成使用
static保存在class实例的尾部,在堆中
3.和c语言c++有点像
package com.demo.static_;import java.sql.sqloutput;public class childgeme {    public static void main(string[] args) {        child ch01=new child("牛牛牛");        ch01.join();        ch01.count++;        child ch02=new child("马马马");        ch02.join();        ch02.count++;        child ch03=new child("猪猪猪");        ch03.join();        ch03.count++;        system.out.println("共有"+child.count+"个小孩加入了游戏");        system.out.println("ch01.count="+ch01.count);        system.out.println("ch02.count="+ch02.count);        system.out.println("ch03.count="+ch03.count);    }}class child{    private string name;    //定义一个变量count,是一个类变量(静态变量)    public static int count=0;    public child(string name) {        this.name = name;    }    public string getname() {        return name;    }    public void setname(string name) {        this.name = name;    }    public static int getcount() {        return count;    }    public static void setcount(int count) {        child.count = count;    }    public void join(){        system.out.println(this.name+"加入了游戏");    }}
2.类变量(静态变量的访问)静态变量的访问权限和范围和普通属性是一样的
package com.demo.static_;import java.sql.sqloutput;public class visit_static {    public static void main(string[] args) {        //1.类名.类变量名        //static类变量在类加载的时候就生成使用        system.out.println("a.name="+a.name);        system.out.println("a.getage()="+a.getage());        //2.类对象.类变量名        a a=new a();        system.out.println("a.name="+a.name);        system.out.println("a.getage()="+a.getage());    }}class a{    //类变量    public static string name="demo龙";    private static int age=99;    public static int getage() {        return age;    }    public static void setage(int age) {        a.age = age;    }}
若类变量是private,则主函数中无法访问,需借助类函数访问
3.类方法1.类方法也叫静态方法
2.访问修饰符+static+数据返回类型(){}
3.static+访问修饰符+数据返回类型(){}
4.调用方法和类方法相同
package com.demo.static_;public class static_method {    public static void main(string[] args) {        stu a01=new stu("小虎");        //stu.sumfee();        a01.sumfee(150);        stu a02=new stu("小龙");        a02.sumfee(250);        //stu.sumfee();        stu a03=new stu("小猪");        stu.sumfee(199);        //输出当前收到的总学费        stu.showfee();    }}class stu{    private string name;//普通成员    //定义一个静态变量来累计学生的学费    private static double fee=0;    public stu(string name) {        this.name = name;    }    public string getname() {        return name;    }    public void setname(string name) {        this.name = name;    }    //当方法使用了static修饰后,该方法就是静态方法    //静态方法就可以访问静态变量    public static double getfee() {        return fee;    }    public static void setfee(double fee) {        stu.fee = fee;    }    public static void sumfee(double fee){        stu.fee+=fee;    }    public static void showfee(){        system.out.println("总学费="+stu.fee);    }}
detail
1.静态方法只能访问静态成员,this/super都不允许在类方法使用
2.非静态方法,可以访问静态成员和非静态成员
3.都遵守访问权限
以上就是java类变量和类方法实例分析的详细内容。
   
 
   