java面向对象(八) static关键字

1.使用static定义属性

一个类中,主要由属性和方法组成,对每一个对象都分别拥有各自的属性内容,如果现在需要把类中的属性定义为公共属性,,则可以在声明属性前加上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 String getInfo(){
		return "姓名:" + this.name +",年龄:" + this.age + ",城市:" + this.country ;
	}//getter,setter略
}
public class TestDemo{
	public static void main(String args[]){
		Person per1 = new Person("张三",20) ;
		Person per2 = new Person("李四",30) ;
		per1.country = "北平" ;
		System.out.println(per1.getInfo()) ;
		System.out.println(per2.getInfo()) ;
	}
}
运行结果:


以上程序中的static属性保存在了全局数据区,如果有一个对象修改了country属性的内容,一定会影响其他对象的country属性内容.

但是显然由某一个对象修改时不合适的,应该由所有对象的公共代表——类来进行操作,即"类名称.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 String getInfo(){
		return "姓名:" + this.name +",年龄:" + this.age + ",城市:" + this.country ;
	}//getter,setter略
}
public class TestDemo{
	public static void main(String args[]){
		Person per1 = new Person("张三",20) ;
		Person per2 = new Person("李四",30) ;
		Person.country = "北平" ;
		System.out.println(per1.getInfo()) ;
		System.out.println(per2.getInfo()) ;
	}
}
运行结果:


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 String getInfo(){
		return "姓名:" + this.name +",年龄:" + this.age + ",城市:" + this.country ;
	}//getter,setter略
}
public class TestDemo{
	public static void main(String args[]){
		Person.country = "北平" ;
		System.out.println(Person.country) ;
	}
}
运行结果:


以上程序的原因在于:普通属性保存在堆内存里,而static属性保存在全局数据区中.static虽然定义在类中,但是它是独立于类之外的.

2.使用static定义方法

使用static定义的而方法也可以在没有实例化对象产生的情况下又类名称直接调用.

范例4:

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){
		country = c ;
	}
	public static String getCountry(){
		return country ;
	}
	public String getInfo(){
		return "姓名:" + this.name +",年龄:" + this.age + ",城市:" + this.country ;
	}//getter,setter略
}
public class TestDemo{
	public static void main(String args[]){
		Person.setCountry("北平") ;
		System.out.println(Person.getCountry()) ;
	}
}
运行结果:


对于static方法和非static方法有如下限制:

    static定义的方法不能调用非static方法定义的方法或属性;

    非static定义的方法可以调用static的属性或方法.

这样限制的原因如下:

    使用static定义的属性和方法,可以在没有实例化对象的时候使用;

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

3.理解主方法

范例5:

public class TestDemo{
	public static void main(String args[]){
		print();
	}
	public static void print(){
		System.out.println("helloworld") ;
	}
}
运行结果:


对于以上程序,实际上表示的是一个static方法调用其他的static方法,但是如果print()方法没有static呢?

则会出现如下错误:


所以必须使用实例化对象来调用非static方法,即所有的非static方法都要由实例化对象调用.范例如下:

public class TestDemo{
	public static void main(String args[]){
		new TestDemo().print() ;
	}
	public void print(){
		System.out.println("helloworld") ;
	}
}
运行结果:


对于主方法:

    public:是一种访问权限,表示公共;

    static:此方法由类名称直接调用,执行类:java 类名称;

    void:主方法是一切的开始;

    main:系统规定的一个方法名称,执行类的时候默认找到此名称;

    String args[]:表示的是一些运行时参数,通过字符串接收;范例如下:

public class TestDemo{
	public static void main(String args[]){
		for(int x = 0 ; x < args.length ; x++){
			System.out.println(args[x]) ;
		}
	}
}

所有的输入属性在执行类时使用空格分割,如"java TestDemo 参数 参数 参数...."

运行结果:


如果要输入的参数本身就有空格,则可以使用 " 声明,例如:java TestDemo "hello world" "hello csdn" 

4.static 关键字的使用

在开发中使用static关键字定义属性或方法的原因:

    希望在没有实例化对象时可以执行类的某些操作;

    希望表示出数据共享的概念;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值