1.static關鍵字
我們在程序編寫中最常出現的static地方就是public static void main(String [] args) 這個主方法就使用了static關鍵字。
2.用static定義屬性
例如,現在要定義一個類,該類實現的是北京人的包裝。
屬性包括:姓名,年齡,城市。
由於所有人都是北京人,所以城市大家的屬性都是相同的。
案例:
//本程序是對static進行測試
class Beijing
{
//封裝屬性
private String name;
private int age;
private String city = "北京";
//構造方法
public Beijing(String name, int age)
{
this.name = name;
this.age = age;
}
public String getInfo()
{
return "姓名:" + this.name + ",年齡:"+ this.age + ",城市:" + this.city;
}
}
public class TestStatic
{
public static void main(String[] args)
{
Beijing per1 = new Beijing("張三",10);
Beijing per2 = new Beijing("張三",20);
Beijing per3 = new Beijing("張三",30);
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
}}
結果
如果要把city進行修改,那麼會相當的麻煩。由於每一個對象都開闢了各自的city空間屬性,要分佈賦值,數據量特別大、重複率也很高。所以在封裝屬性的時候把city作爲公共屬性,用static定義那麼修改就簡單了。
//本程序是對static進行測試
class Beijing
{
//封裝屬性
private String name;
private int age;
private static String city = "北京";
//構造方法
public Beijing(String name, int age)
{
this.name = name;
this.age = age;
}
public String getInfo()
{
return "姓名:" + this.name + ",年齡:"+ this.age + ",城市:" + this.city;
}
public void setCity(String city)
{
this.city = city;
}
}
public class TestStatic
{
public static void main(String[] args)
{
Beijing per1 = new Beijing("張三",10);
Beijing per2 = new Beijing("張三",20);
Beijing per3 = new Beijing("張三",30);
per1.setCity("天津");
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
}
}
結果:
內存說明:
1.不使用static
2.不使用static
使用static關鍵字進行聲明,內容不再保存在堆中,而是保存在全局數據中,這是每個對象都共同擁有的,那麼某個對象要對其進行操作是否合理呢?
所以,需要最高領導—類名直接調用,可以在不產生對象的情況下由類名調用。
//本程序是對static進行測試
class Beijing
{
//封裝屬性
private String name;
private int age;
static String city = "北京";
//構造方法
public Beijing(String name, int age)
{
this.name = name;
this.age = age;
}
public String getInfo()
{
return "姓名:" + this.name + ",年齡:"+ this.age + ",城市:" + this.city;
}
public void setCity(String city)
{
this.city = city;
}
}
public class TestStatic
{
public static void main(String[] args)
{
Beijing per1 = new Beijing("張三",10);
Beijing per2 = new Beijing("張三",20);
Beijing per3 = new Beijing("張三",30);
Beijing.city="山東"; //沒有產生對象的情況下直接調用
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
}
}
3.static方法
Static 可以在不產生實例化對象的情況下用類名稱直接調用,同static屬性。
注意:
1.static聲明的方法不能調用非static屬性和方法
2.非static聲明可以調用static方法和屬性
原因:非static的屬性在對象實例化後才能開闢使用,static可以不進行實例化使用。
案例:
//本程序是對static進行測試
public class TestStatic
{
public static void main(String[] args)
{
print();
}
public static void print()
{
System.out.println("hello world !");
}
}
static調用static方法
但是修改就出現問題
//本程序是對static進行測試
public class TestStatic
{
public static void main(String[] args)
{
print();
}
public void print()
{
System.out.println("hello world !");
}
}
這是相當於類中聲明瞭一個普通的方法,要想使用,必須建立對象才能調用。
//本程序是對static進行測試
public class TestStatic
{
public static void main(String[] args)
{
TestStatic t1 = new TestStatic();
t1.print();
}
public void print()
{
System.out.println("hello world !");
}
}
4.主方法
傳遞參數String[] args 所有的參數在傳遞的時候通過空格分割。
//本程序是對static進行測試
public class TestStatic
{
public static void main(String[] args)
{
for (int i=0;i<args.length ;++i )
{
System.out.println(args[i]);
}
}
}
5.Static 方法的使用
1.不希望或者不能產生實例化對象就要操作類中的方法, 那麼該方法定義爲static
2.某些數據需要共享
統計實例化對象的個數
//本程序是對static進行測試
class Person
{
private String name;
private int age;
private static int count;
public Person(String name, int age)
{
++count;
System.out.println("產生了第" + i + "個實例化對象");
}
}
public class TestStatic
{
public static void main(String[] args)
{
Person per1 = new Person("Tyrion",20);
}
}
//類中某個對象自動命名
//本程序是對static進行測試
class Person
{
private String name;
private static int count;
public Person()
{
this("空" + ++count);
}
public Person(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
public class TestStatic
{
public static void main(String[] args)
{
Person per1 = new Person("Tyrion");
System.out.println(per1.getName());
System.out.println(new Person().getName());
}
}
祝大家健健康康,快快樂樂。