Java-关于static

本文探讨了Java中对象的生命周期、内存管理机制以及static关键字的使用。详细解释了对象如何在内存中分配和释放,并重点介绍了static字段和方法的特点及应用场景。

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

 private PassParam paramObj = new PassParam();前面没有必要加private,因为paramObj在作用域终点就消失了。
 Java对象不具备和基本类型一样的生命周期,当用new创建一个Java对象时,它可以存活于作用之外
 {
  String s = new String("a String");
 }
 引用s在作用域终点就消失了。然而s指向的String对象继续占据内存空间(内存泄漏),直至"垃圾回收器"将其回收。
 static StaticTest
 {
  static int i = 47;
  StaticTest st1 = new StaticTest();
  StaticTest st2 = new StaticTest();
  StaticTest.i++;
 }
 StaticTest.i和任意StaticTest对象指向同一份存储空间,它们共享同一个i的堆地址。StaticTest.i++执行完毕,st1.i,st2.i,StaticTest.i的值全变为48.
 static字段对每个类只有一份存储空间,而非static类对每个对象有一份存储空间.static方法重要用处在于不创建对象的前提下就可以调用它.
 希望无论是否产生对象or产生了多少个对象,某些特定数据在内存里只有一份时,使用static字段.eg:所有中国人都有个国家名称,每个中国人都共享这个国家名称.
 
 当申明一个事物是static,就意味着这个数据和方法不会与包含它的那个类的任何对象实例关联在一起;而非static事物必须知道它们一起运作的特定对象.
 所以对于static方法,不能通过调用其他非static成员或方法而没有指定某个命名对象,来直接访问非static成员或方法.
 
 在静态方法里只能直接调用其它的静态成员(变量,对象和方法),而不能直接访问类中的非静态成员(同类中调用不用加类名;其它类中调用:类名.静态成员or对象名.静态成员).
 因为对于非静态成员,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象.
 静态方法不能以任何方式引用this和super关键字,理由同上,当静态方法被调用时,this所引用的对象根本就没有产生.
 
 System:类名,out:静态对象,println:out是PrintStream类的static对象,println是PrintStream类的方法.
 
 相反,非静态方法里可以调用任何的静态方法和非静态方法

 

class Chinese{
    
static String country = "CHINA";
    
static int count = 0;
    String name;
    
int age;
    
    Chinese(String name,
int age){
        
this.name = name;
        
this.age = age;
        count
++;//用于统计Chinese总共产生了多少个对象(count为static的,放在构造方法中,很巧妙).
    }

    
    
void singOurCountry(){
        System.out.println(name 
+ " is singing our country:" + country);
    }

}


class TestChinese{
    
public static void main(String[] args){
        Chinese person1 
= new Chinese("张三",20);
        Chinese person2 
= new Chinese("李四",30);
        Chinese person3 
= new Chinese("王五",40);
        
        System.out.println(
"All chinese is singing our country:" + Chinese.country);
        person1.singOurCountry();
        person2.singOurCountry();
        person3.singOurCountry();
    }

}

 

class PassParam1{
    
int i;
    PassParam1 paramObj 
= new PassParam1();
    
public static void main(String[] args){
        paramObj.i 
= 500//Cannot make a static reference to the non-static field paramObj
    }

}

main方法是static的,如果没有指定对象,就不能调用非static成员PassObj.修改:
 方法1
 static PassParam1 paramObj = new PassParam1();
 public static void main(String[] args){paramObj.i = 500}
 方法2
 PassParam1 paramObj = new PassParam1();
 public static void main(String[] args)
 {
      PassParam1 Obj = new PassParam1();
      Obj.paramObj.i = 500;
 }
 方法3
 public static void main(String[] args)
 {
      PassParam1 paramObj = new PassParam1();
      paramObj.i = 500;
 }

 

class PassParam2{
    
int i;
    
public static void main(String[] args){
        PassParam2 paramObj2 
= new PassParam2();
        paramObj2.i 
= 500;
        changeParam(paramObj2.i);
        
/*Cannot make a static reference to the non-static method changeParam(int) from the type PassParam2
         * 对于static方法,不能通过调用其他非static方法而没有指定某个命名对象
         * 对于非static方法,可以通过调用其他非static方法而没有指定某个命名对象,非static方法在使用前是要求实例化的
*/

        changeParam(paramObj2);
    }

    
public void changeParam(int param){
        param 
= 30;
    }

    
public static void changeParam(PassParam2 paramObj){
        paramObj.i
= 300;
    }

}

 

class PassParam3{
    
int param;
    
public static void main(String[] args){
        param 
= 50;
        
/*Cannot make a static reference to the non-static field param*/
    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值