static方法和实例方法的区别
♏️静态方法:可直接通过类名访问
1️⃣静态方法中不能 使用this和super;
2️⃣不能 直接访问所属类的实例变量和实例方法;
3️⃣可直接访问类的静态变量和静态方法。
♏️实例方法:通过实例访问
1️⃣可直接访问所属类的静态变量、静态方法、实例变量和实例方法。
♏️静态方法必须被实现
解释
???
为什么静态方法中不能用 this 和 super,不能访问所属类的实例变量和实例方法呢?
这是因为,我们假设它能用this,可以用实例变量和实例方法,this是指当前对象,而静态方法可以通过类名直接调用,不用new对象,既然可以通过类名直接调用,就说明和对象无关, 既然和对象无关,那静态方法就不知道this这个当前对象是谁,这就和假设能用this矛盾了,所以不能用this。
同理,实例变量应该和实例相关,和对象相关,而用static定义的方法可以和对象无关,它是和类相关的, 直接通过类名就能调用,一切通过对象调用的都和它无关,它没法用。
所以说静态的方法只能被类调用。
静态方法必须被实现?
即静态方法不能是抽象的,一般我们写的静态方法都是带大括号的,带方法体的,没方法体的叫抽象方法(后面讲抽象类时再细讲),现在只需记住静态方法都得带大括号(带方法体)。
为什么main方法得是static的?
main方法的作用是作为程序的入口,我们把main方法定义成static之后,Java虚拟机只要加载了main方法所在的类(Java虚拟机会先加载类),就会找到静态方法(因为静态方法是跟类相关联的)去执行,而不需要先去创建对象。因此main方法是程序的入口。main方法前的static不能去掉。
用法示例
1、实例方法可以访问的东西:
package com.kgc.stu;
public class Student {
//实例变量
private int age;
//类变量(静态变量)
public static String email;
//实例方法1(普通的方法)
public void method2(){
}
//静态方法
public static void method3(){
}
//实例方法2(普通的方法)
public void method1(){
System.out.println(age); //实例方法里可以访问实例变量
System.out.println(email); //实例方法里可以访问静态变量,静态变量在MyEclipse里会显示为斜体样式。
this.method2(); //实例方法可以直接访问实例方法
method2(); //上一行的this.method2()可以简写成method2()
method3(); //实例方法可以直接访问静态方法
}
}
2、实例方法被别人访问:
package com.kgc.stu;
public class Test {
public static void main(String[] args) {
Student stu = new Student(); //先创建Student对象stu(即一个实例)
System.out.println(stu.name); //通过Student对象stu(即一个实例)来调用实例变量name
stu.method1(); //通过Student对象stu(即一个实例)来调用实例方法method1()
stu.method3(); //可以通过实例调用静态方法method3(),但不提倡,因为可以直接通过静态的方法调用(即通过类名调用)。
Student.method3(); //静态方法method3()可以被类名直接调用,静态方法在MyEclipse中会显示为斜体样式。
Math.random(); //直接通过Math这个类来调用静态方法random()
Scanner input = new Scanner(System.in); //先创建Scanner的对象input
input.next(); //再通过对象input调用next()方法
// Scanner.next(); //直接通过类名Scanner调用不了next()方法
//说明next()方法不是静态方法,而是实例方法。
}
}
3、静态方法:
package com.kgc.stu;
public class Student {
//实例变量
private int age;
//类变量(静态变量)
public static String email;
//静态方法
public static void method2(){
}
//静态方法
public static void method3(){
//System.out.println(this.age); // 静态方法里不能用this
//System.out.println(age); // 静态方法里不能直接访问所属类的实例变量
System.out.println(email); // 静态方法里可以直接访问类的静态变量
//method1(); // 静态方法里不能直接访问所属类的实例方法
method2(); // 静态方法里可以直接访问类的静态方法
}
//实例方法(普通的方法)
public void method1(){
System.out.println(this.age); //普通的方法里可以用this访问实例变量
this.method2(); //普通方法里可以用this访问实例方法
System.out.println(this.email); //普通方法里可以用this访问静态变量,但建议把this换成类名(这里是Student),或直接把this去掉。
this.method3(); //普通方法里可以用this访问静态方法,但建议把this换成类名(这里是Student),或直接把this去掉。
}
}
4、静态方法的调用:
package com.kgc.vote;
/*
* 模拟实现选民投票过程:一群选民进行投票,每个选民只允许投一次票,并且当投票总数达到100时,就停止投票。
* */
//选民类
public class Voter {
private static int count;
private static final int MAX_COUNT = 100;
private String name;
public Voter(){}
public Voter(String name){
this.name = name;
}
//投票
public void voteFor(){
if(count == MAX_COUNT){
System.out.println("投票活动已经结束!");
return; //结束掉当前方法
}else{
count++;
System.out.println(this.name + ",感谢您投票!");
}
}
//打印投票结果(这里我们使用静态方法,如果使用普通方法也没问题,但在测试类里就得先创建对象,再通过对象来调用,)
public static void printResult(){
System.out.println("选民投票总数:" + count);
}
}
package com.kgc.vote;
//测试选民投票
public class TestVoter {
public static void main(String[] args){
Voter zhang = new Voter("zhangsan");
zhang.voteFor();
Voter li = new Voter("lisi");
li.voteFor();
Voter wang = new Voter("wangwu");
wang.voteFor();
//如果把Voter类里的printResult方法前的static去掉了,就按下面两行代码调用printResult方法。
//Voter res = new Voter(); //先创建对象
//res.printResult(); //再通过对象调用普通方法(不能通过类名调用)
Voter.printResult(); // 静态方法通过类名就可以调用
for(int i = 1; i <= 97; i++){
Voter v = new Voter("v" + i);
v.voteFor();
}
Voter.printResult();
Voter v101 = new Voter("v101");
v101.voteFor();
Voter.printResult();
}
}
PS
点击这里查看本文开头部分用到的Emoji表情符号参考手册?(但部分emoji不支持,并且实际样式有差异)
经过测试,csdn的markdown编辑器中部分可用的emoji如下表所示:
emoji | emoji code |
---|---|
? | :smile: |
? | :laughing: |
? | :blush: |
? | :smiley: |
? | :smirk: |
? | :heart_eyes: |
? | :kissing_heart: |
? | :kissing_closed_eyes: |
? | :flushed: |
? | :relieved: |
? | :satisfied: |
? | :grin: |
? | :wink: |
? | :stuck_out_tongue_winking_eye: |
? | :stuck_out_tongue_closed_eyes: |
? | :grinning: |
? | :kissing: |
? | :kissing_smiling_eyes: |
? | :stuck_out_tongue: |
? | :sleeping: |
? | :worried: |
? | :frowning: |
? | :anguished: |
? | :open_mouth: |
? | :grimacing: |
? | :confused: |
? | :hushed: |
? | :expressionless: |
? | :unamused: |
? | :sweat_smile: |
? | :sweat: |
? | :disappointed_relieved: |
? | :weary: |
? | :pensive: |
? | :disappointed: |
? | :confounded: |
? | :fearful: |
? | :cold_sweat: |
? | :persevere: |
? | :cry: |
? | :sob: |
? | :joy: |
? | :astonished: |
? | :scream: |
? | :tired_face: |
? | :angry: |
? | :rage: |
? | :triumph: |
? | :sleepy: |
? | :yum: |
? | :mask: |
? | :sunglasses: |
? | :dizzy_face: |
? | :imp: |
? | :smiling_imp: |
? | :neutral_face: |
? | :no_mouth: |
? | :innocent: |