java中的 static 方法中返回数组

package HelloWorld;
import java.util.Arrays ;
public class  hi {	    
	public static void main(String[] args) {        

	//hi hello = new hi();        

	int avg[] = hi.calcAvg() ;        //类.方法   或者可以int avg[] = calcAvg() ; 
	System.out.println(Arrays.toString(avg));     //打印数组   
	System.out.println(avg);                //此处打印的为数组的第一个元素的地址,即数组的内存地址
	}

	public static int[] calcAvg() {
	int test[] = {1,2,3,5,74,8,9,6,64,153,4685,13,4684,46} ;			
	return test ;	        
	}
}


package HelloWorld;
import java.util.Arrays ;
public class  hi {	    
	public static void main(String[] args) {        

	hi hello = new hi();        

	int avg[] = hello.calcAvg() ;        //对象.方法
	System.out.println(Arrays.toString(avg));        //打印数组 
	System.out.println(avg);                //此处打印的为数组的第一个元素的地址,即数组的内存地址
	}

	public int[] calcAvg() {
	int test[] = {1,2,3,5,74,8,9,6,64,153,4685,13,4684,46} ;			
	return test ;	        
	}
}


        使用static静态方法返回数组,在引用时直接引用方法而不用创建对象来引用,其过程与面向过程编程相似,

缺点在于程序运行过程中内存不释放,不能大量这样引用(对于面向对象的编程语言而言,不建议这样做?)

        在使用静态方法时可以使用对象引用方法,会出现警告:The static method calcAvg() from the type hi should be accessed in a static way ;但是不能不使用静态方法就直接调用方法。

  1. static静态方法,直接使用"类.方法",因为静态方法在对象创建前就存在了,他的使用不依赖对象是否被创建

  2. 非静态的方法用"对象.方法"的方式,因为他在对象创建前不存在,必须依赖对象的创建后,才能使用


        



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值