十 java面向对象程序设计(static关键字)

本文深入探讨Java中的静态特性,包括静态变量、静态方法的特点及使用场景,并解释了静态代码块的作用,最后通过示例展示了静态元素如何工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

十 java面向对象程序设计(static关键字)


/**
 * 面向对象之五
 * static关键字总结
 */
class Static
{
 /**
  * 1,静态变量:
  * a,在变量前面加上static关键字后,该变量为静态变量,
  *   这时,无论是否产生了对象或无论产生了多少对象的情况下,这个特定的变量在内存空间里只有一份
  * b,静态的变量在调用时,可以不用产生对象,直接通过"类名.变量名"这种形式进行调用.
  */
 static int count1 = 0;//定义一个静态变量用于计数
 int count2 = 0;//定义一个非静态变量,用于对比
 public Static()
 {
  System.out.println("new Static()");
  System.out.println("static var: " + (++count1));//每次创建该类对象时,静态变量开始计数
  System.out.println("no static var: " + (++count2));//添加一个非静态变量,同样用于计数,对比效果.
 }
 /**
  * 2,静态方法
  * a,和静态变量相同,静态方法同样可以在不创建对象下,通过类名直接调用.
  * b,在静态方法里只能直接调用同类中其它的静态成员(包括变量和方法),而不能直接访问类中的非静态成员。
  *   这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象。
  * c,静态方法不能以任何方式引用this和super关键字,因为静态方法在使用前不用创建任何实例对象,
  *   当静态方法被调用时,this所引用的对象根本就没有产生。
  */
 public static void getInfo()//静态方法,getInfo()
 {
  System.out.println("equals static function & no static function");
 }
 public void getInfo(int count1,int count2)//动态方法,重载getInfo()
 {
  Static.count1 = count1;
  //this.count1 = count1;//也可以通过this调用静态变量count1
  this.count2 = count2;
  getInfo();//在非静态方法中可以直接调用静态方法.
 }
 public void function()
 {
  System.out.println("function is calling...");
 }
 public static void function(int count1)
 {
  Static.count1 = count1;
  //function();//错误,不能在静态方法中调用非静态的方法function();
  new Static().function();
 }
 public static void function(int count1,int count2)
 {
  function(count1);//但是,在静态方法中可以直接调用静态方法,和静态变量.
  //this.count2 = count2;
  //错误,静态方法中没有this和super,因为this是指向对象的,但静态方法在使用时根本无需产生对象,
  //所以this无法引用这个没有产生的对象
  Static s = new Static();
  s.count2 = count2;//静态方法不能访问非静态的成员变量,所以必须先产生对象.
 }
 //总结:静态变量在内存中只有一份,静态方法和静态变量一样,在使用前不用创建任何实例对象,通过类名直接调用.
 //    所以静态方法中没有this和super关键字.
 //    静态方法不可以访问非静态的变量和方法,但是非静态方法可以访问静态的方法和变量.
 /**
  * 3,main()方法:
  * main() 方法是静态的,因此JVM在执行main方法时不创建main方法所在的类的实例对象,
  * 因而在main()方法中,我们不能直接访问该类中的非静态成员,必须创建该类的一个实例对象后,
  * 才能通过这个对象去访问类中的非静态成员
  */
 public static void main(String[] args)
 {
  /*静态成员变量和动态成员变量的对比*/
  System.out.println("静态成员变量和动态成员变量的对比");
  new Static();
  new Static();
  new Static();
  //创建3个对象.静态变量count1值为3,证明无论创建多少对象,静态方法只占用一份内存.
  //但非静态方法count2值每次都是1,证明每当新建一个对象时,就新建了一个count2的副本.
  
  /*静态成员变量调用时可以不用产生对象*/
  System.out.println("静态成员变量调用时可以不用产生对象");
  System.out.println(new Static().count1);
  System.out.println(new Static().count2);
  System.out.println(Static.count1);
  //System.out.println(Static.count2);必须通过对象调用非静态成员变量
  
  /*调用静态方法*/
  System.out.println("调用静态方法");
  Static s = new Static();
  s.getInfo(10, 20);
  
  Static.function(10, 20);
 }
}
public class StaticTest {
 /**
  * 4,静态代码块
  * 静态块经常用来进行类属性的初始化。
  * 类中的静态代码块被自动执行,尽管我们产生了类的多个实例对象,但其中的静态代码块只被执行了一次。
  */
 static int count1 = 0;
 static int count2 = 0;
 //分别定义两个静态变量用于计数.
 static//静态代码块
 {
  count1++;
  System.out.println("static code block is calling...");
 }
 {//非静态代码块
  count2++;
  System.out.println("code block is calling...");
 }
 public static void main(String[] args)
 {
  /*静态代码块*/
  new StaticTest();
  new StaticTest();
  new StaticTest();//new了3个对象,静态代码块只执行一次,而非静态代码块执行了3次.
  System.out.println(count1);
  System.out.println(count2);
 }
}
/* output from Static main()
静态成员变量和动态成员变量的对比
new Static()
static var: 1
no static var: 1
new Static()
static var: 2
no static var: 1
new Static()
static var: 3
no static var: 1
静态成员变量调用时可以不用产生对象
new Static()
static var: 4
no static var: 1
4
new Static()
static var: 5
no static var: 1
1
5
调用静态方法
new Static()
static var: 6
no static var: 1
equals static function & no static function
new Static()
static var: 11
no static var: 1
function is calling...
new Static()
static var: 12
no static var: 1
 */

/* output from StaticTest main()
static code block is calling...
code block is calling...
code block is calling...
code block is calling...
1
3
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值