在类级别和方法中声明对象之间的区别 [英] difference between declaring an object on class level and in methods

查看:91
本文介绍了在类级别和方法中声明对象之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A 是测试之外的另一类

情况1:

public class Test{



      A testObj;

      public static void main(String[] args){
         testObj=new A();
         testObj.methodInsideClassA();
      }
}

情况2:

 public class Test{


      public static void main(String[]  args){
         A testObj = new A();
         testObj.methodInsideClassA();
      }
}

那么它们之间有什么区别?我应该使用什么?

so whats the difference between them? And what should I use?

推荐答案

案例1



testObj是

Case 1

testObj is a class-level variable.

testObj是局部变量。
局部变量是在方法或构造函数中声明的变量。

testObj is a local variable. A local variable is the one that is declared within a method or a constructor.

类级变量与局部变量之间的一个重要区别是访问说明符只能应用于类级变量,不能应用于局部变量。

One important distinction between class-level variable and local variable is that access specifiers can be applied to class-level variables only and not to local variables.

这篇关于在类级别和方法中声明对象之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆