java-static和final

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:表示不可修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值