类变量与类方法,咱们习惯也将其称为静态变量与静态方法。java
经过一个实际案例来了解,为何须要使用静态变量。数组
声明一个学生类,每建立一个学生对象,统计学生的人数。this
public class Test{ public static void main(String[] args){ int count = 0; //局部变量,累积计数 Student stu1 = new Student("小范"); count ++; Student stu2 = new Student("小黄"); count ++; Student stu3 = new Student("小雨"); count ++; System.out.println("目前学生人数有:" + count); } } class Student{ private String name; public Student(String name){ this.name = name; } }
经过这个案例咱们能够看到,count做为一个局部变量,独立于Student外,固然也和建立的学生对象没有任何关联。之后在访问count的时候会很麻烦,在Student类中访问count也很麻烦。spa
指针
package static_args_methods.args; public class Static_args { public static void main(String[] args) { Student student = new Student("小范"); System.out.println(student.showFee()); // 100 Student student2 = new Student("小黄"); System.out.println(student2.showFee()); //200 // System.out.println(Student.fee); //私有属性,没法访问 System.out.println(Student.count); //2 } } class Student{ private String name; //static + 访问修饰符 + 类型 + 属性名 static private double fee; //学费 static Blackboard bb; //黑板 //访问修饰符 + static + 类型 + 属性名 (推荐) public static int count; //人数 public Student(String name) { super(); this.name = name; fee += 100; //每一个学生交100学费 count += 1; } public double showFee(){ return fee; } } class Blackboard{ }
由此案例,引出类变量/静态变量的概念及使用。对象
类变量也叫静态变量/静态属性,是该类的全部对象共享的变量,任何一个该类的对象去访问它时,取到的都是相同的值,一样任何一个该类的对象去修改它时,修改的也是同一个变量。blog
访问修饰符 static 数据类型 变量名; [推荐] 生命周期
static 访问修饰符 数据类型 变量名; 虚拟机
类名.类变量名 [推荐]it
或者 对象名.类变量名 【静态变量的访问修饰符的访问权限和范围 和 普通属性是同样的。】
当须要一个类的全部对象均可以共用一个属性时,就能够将此属性设置为静态属性。例如,Student中的count属性。
被静态static修饰过的属性,在类加载时就会被生成,而后初始化。
建议使用“类名.属性名” 的方式访问静态属性,由于静态属性(类变量)的生命周期是随类的加载开始,随着类消亡而销毁。在类加载的时候,并无对象被建立,此时也能够使用类变量。
类变量是全部对象共享使用的,而普通变量是每一个对象都独有的。
类变量的使用,访问权限也是由访问修饰符修饰的。例如:Student 的类变量 count 是 private 修饰的,那么在 Static_args 类中也不能经过 Student.count 来访问。
类方法也叫静态方法 。
形式以下:
访问修饰符 static 数据返回类型 方法名() { } 【推荐】
static 访问修饰符 数据返回类型 方法名() { }
类方法的调用:
使用方式: 类名.类方法名 或者 对象名.类方法名 【前提是 知足访问修饰符的访问权限和范围】
类方法与普通方法都是在类加载时就加载了,将其结构信息放在方法区中。
类方法中没有this关键字,也不能使用super关键字。由于类方法在类加载时就能够调用,此时尚未对象被生成。而this与super都是表明对象的关键字。
类方法/静态方法 只能访问静态属性或者调用其余静态方法,不能够使用成员属性与成员方法。由于在类方法被加载时,成员属性与成员方法都尚未被加载出来,不能使用。【前提是 知足访问修饰符的访问权限和范围】
而在成员方法(普通方法)中能够使用全部的静态方法与静态属性。【前提是 知足访问修饰符的访问权限和范围】
但不要误理解为,静态方法中使用的任何属性或方法都必须是静态的。也能够在静态方法中声明普通属性来使用。
public class StaticMethodDetail { public static void main(String[] args) { // TODO Auto-generated method stub //类方法能够经过类名调用,也能够经过对象名调用 new DD().hi(); DD.hi(); } } class EE { public String name = "EE"; } class DD extends EE { /* * 类方法和普通方法都是随着类的加载而加载,将结构信息存储在方法区 :类方法中无this的参数,普通方法中隐含着this的参数 * 类方法中,不能使用this ,普通方法能够使用this */ private int num = 200; private static String name = "老王"; public static void hi() { //System.out.println(this.num); } //类方法中不容许使用和对象有关的关键字,好比this和super public static void ok() { //System.out.println(this.num); //System.out.println(super.name); } //类方法(静态方法)中 只能访问 静态变量 或静态方法 public void m1() { } public static void hello() { ok(); //m1(); System.out.println(name); } //普通成员方法(非静态方法),既能够访问 普通变量(方法),也能够访问静态变量(方法) public void m2() { ok(); m1(); System.out.println(name); System.out.println(num); } }
package static_args_methods.methods; public class Static_methods { public static void main(String[] args) { Person person = new Person("小黄", 2000); Person person2 = new Person("小范", 2500); //能够使用类名调用,也能够使用对象调用 System.out.println(Person.show()); //建议 System.out.println(person2.show()); } } class Person{ private String name; private double salary; //工资 private static double totalSalary; //总工资 public Person(String name, double salary) { super(); this.name = name; this.salary = salary; sumSalary(salary); } //静态方法 public static void sumSalary(double salary) { totalSalary += salary; } //返回总工资(静态方法里只能使用静态属性与方法) public static double show() { // salary = 100; //Cannot make a static reference to the non-static field salary // this.totalSalary = 100; //在静态方法中没有this指针,也不能够使用super return Person.totalSalary; } }
因为java虚拟机须要调用类的main()方法,因此该方法的访问权限必须是public,又由于java虚拟机在执行main()方法时没必要建立对象,因此该方法必须是static的,该方法接收一个String类型的数组参数,该数组中保存执行java命令时传递给所运行的类的参数。
public class TestMain { private String name = "jack"; private static int num = 600; //1.main方法时JVM调用,所以 public static //2. String[] args 是运行程序时,能够输入的 //3. 好比 java TestMain tom jack smith public static void main(String[] args) { for(int i= 0 ; i < args.length;i++) { System.out.println(args[i]); } //m1(); m2(); System.out.println(num); //若是咱们就是要再 main调用非静态的成员,须要先建立对象,再调用.. TestMain tm = new TestMain(); tm.m1(); } public void m1() { } public static void m2() { } }