1. 程式人生 > >java靜態方法和實例方法的區別

java靜態方法和實例方法的區別

區別 -s string chap 變量賦值 sta 方法 url cal

靜態方法(方法前冠以static)和實例方法(前面未冠以static)的區別 (視頻下載) (全部書籍)

調用靜態方法或說類方法時,可以使用類名做前綴,也可以使用某一個具體的對象名;通常使用類名。

static方法只能處理static域或靜態方法。實例方法可以訪問實例域, 靜態域或靜態方法, 記住都行。本章源碼

class StaticTest {
static int a = 4;
static int b = 9;
static void call() {
/*下一句是錯誤的,因為靜態的不能調用實例的方法。*/
//callins();
System.out.println("a = " + a+"馬克-to-win"+Test.c);//靜態方法可以訪問靜態屬性
}
void callins() {
call();
System.out.println("a = " + a+"實例馬克-to-win"+Test.c);//靜態方法可以訪問靜態屬性
}
}
public class Test {
static int c = 43;
public static void main(String args[]) {
/*剛運行到這一步時,debug觀察,StaticTest.a的值就等於4,Test.c的值就等於43,
說明系統在我們的程序一開始時,就會給所有的類變量賦值。如果是對象參考, 就是null,
見photoshop的例子*/
StaticTest se =new StaticTest();
System.out.println("開始觀察StaticTest.a和Test.c");
se.b=5;
StaticTest.call();//靜態方法用類名直接調用

。。。。。。。。。。。。。。。。。。。。
詳情請進:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner2_web.html#StaticInstanceMethodDifference

java靜態方法和實例方法的區別