1.static
static-常量池(共享变量)
静态域:静态域属于类,不属于对象。
静态常量:
访问方式:
类名.属性名
对象.属性名
静态方法:建议使用
(类名.方法)这种方法调用。
不能向对象实施操作的类。
Math.pow(x,a);
表示没有隐式参数,即没有this参数
public class Static_Test {
static int amount=30;
private int id;
private double price;
private String name;
static private String zookpeer;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getZookpeer() {
return zookpeer;
}
public static void setZookpeer(String zookpeer) {
Static_Test.zookpeer = zookpeer;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Static_Test test1=new Static_Test();
test1.setZookpeer("张三");
Static_Test test2=new Static_Test();
System.out.println(test2.getZookpeer());
System.out.println(test2.amount);//静态常量
System.out.println(Static_Test.amount);//获取静态常量
test2.setZookpeer("李四");
System.out.println(Static_Test.getZookpeer());
}
}
编写工具类
public class Way {
private Way()
{
}
/**
* 遍历的方法
*/
public static void bianli(double a[])
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
/**
* 排序的方法
*/
public static void paixu(double a[])
{
double temp;
for(int i=0;i<a.length;i++)
for(int j=0;j<a.length-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}
public class Way_Test {
public static void main(String [] args)
{
double []a={9,5,6,7,10,45,23};
Way.bianli(a);
System.out.println();
Way.paixu(a);
}
}
求创建对象的次数
public class Person {
String name;
String password;
static int i=0;
static int a=0;
{
i++;
System.out.println("对象被创建"+i+"次");
}
static
{
System.out.println("静态方法被创建"+(++a)+"次");
a++;
}
}
public class Person_Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1=new Person();
Person p2=new Person();
Person p3=new Person();
Person p4=new Person();
}
}
2.final:表示不可修改