static关键字

1. 使用static定义属性:(类名称.属性)

代码如下:

class Person
{
    private String name ;
    private int age ;
    static String country = "北京" ;
    public Person(String name, int age){
        this.name = name ;
        this.age = age ;
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age ;
    }
    public String getInfo(){
        return  "姓名:" + this.name + ",年龄:"+ this.age +
            ",城市:"+ this.country ;
    }

}
public class Test
{
    public static void main(String args[]){
            Person per1 = new Person("张三" , 20) ;
            Person per2 = new Person("李四" , 22) ;
            Person per3 = new Person("王五" , 21) ;
            per1.country = "燕京" ;
            System.out.println(per1.getInfo()) ;
            System.out.println(per2.getInfo()) ;
            System.out.println(per3.getInfo()) ;

    }
}

内存图分析:
static关键字_类名

文字分析如下:
Country属性的修改,一定会影响到其他对象,static定义的属性表示公共属性,因此,应由所有对象的集合进行调用,所有对象的集合就是类,即:static属性最好的调用方式,是通过“类名称.属性”的方式进行调用完成。

类名称.属性————//Person.country = “北京” ;

注意:

  • 使用static定义的属性不在堆内存中保存,保存在全局数据区
  • 使用static定义的属性表示类属性,类属性可以由类名称直接调用;
  • Static属性虽然定义在类之中,但是可以在没有实例化对象的时候进行调用(普通属性保存在堆内存之中,但是static保存在全局数据区之中)。

提示:在以后的开发之中,首先想到的是定义普通属性,在后面编程过程中在根据需要添加。

2. 使用static定义方法

格式:

public static void setCountry(String c){        //Static定义方法
        country = c ;
}

代码如下:

class Person
{
    private String name ;
    private int age ;
    private static String country = "北京" ;
    public Person(String name, int age){
        this.name = name ;
        this.age = age ;
    }   
    public static void setCountry(String c){   //Static定义方法
        country = c ;
    }
    public static String getCountry(){
        return country ;
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age ;
    }
    public String getInfo(){
        return  "姓名:" + this.name + ",年龄:"+ this.age +
            ",城市:"+ this.country ;
    }

}
public class Test
{
    public static void main(String args[]){

            Person.country = "燕京" ;     //类.static方法()
            System.out.println(per1.getInfo()) ;
            Person per1 = new Person("张三" , 20) ;
            System.out.println(per1.getInfo()) ;

    }
}

static分为两派:static派、非static派

  • static定义的方法不能用非static的方法和属性
  • 非static的属性和方法,必须实例化对象之后才可以进行调用

3. 主方法(public static void main(String args[]))

代码如下:

public class Test
{
    public static void main(String args[]){

            print() ;  //直接调用

    }
    public static void print(){
        System.out.print("Hello World !") ;
    }
}


public class Test
{
    public static void main(String args[]){

            new.Test().print() ;  //直接调用

    }
    public void print(){
        System.out.print("Hello World !") ;
    }
}

主方法的组成:

  • public::是一种访问权限,表示公共;
  • static:此方法由类命名称直接调用,执行类:java 类名称;
  • void: 主方法是一切的开始;
  • main:系统规定的一个方法名称,执行类的时候默认找到此名称;
  • String args[]:表示的是一些运行时的参数,通过字符串接收。

接受参数:

public class Test
{
    public static void main(String args[]){

        for(int x = 0 ; x < args.length ; x ++){
            System.out.println(args[x]) ;
        }
    }
}

static关键字_Java_02

4. static关键字的使用

使用static的情况:

  • 希望可以再没有实例化对象的时候可以轻松的执行类的某些操作;
  • 希望数据库共享。

范例:统计一个类产生多少个实例化对象,现在增加一个统计操作,可以统计出一个类之中的所产生的实例化对象的个数,如果现在要产生新的实例化对象,则一定会调用类之中的构造方法,所以可以再构造方法中增加统计,并且这个统计的变量,应该是所有对象共享的,那么应该将其定义为static属性。

代码如下:

class Person
{
    private static int count = 0 ;
    public Person(){                //构造方法一定会被调用
        System.out.println("对象的个数:"+ ++count) ;
    }
}

public class Test
{
    public static void main(String args[]){
            Person per1 = new Person() ;
            Person per2 = new Person() ;
            Person per3 = new Person() ;
    }
}

static关键字_类名_03

范例:为属性自动命名

class Book
{
    private static int count = 0 ;
    private String title ;
    public Book(){  
        this("NOTTITLE:" + count++) ;
    }
    public Book(String title){
        this.title = title ;
    }
    public String getTitle(){
        return this.title ;
    }
}

public class Test
{
    public static void main(String args[]){
        System.out.println(new Book().getTitle()) ;
        System.out.println(new Book("java SE").getTitle()) ;
        System.out.println(new Book().getTitle()) ;

    }
}

static关键字_实例化_04