简体   繁体   English

在 main 方法中声明的变量是局部变量吗?

[英]Variables declared inside main method are local variables?

I want to ask you if a variable declared inside main method is an instance variable or a local variable.请问在main方法中声明的变量是实例变量还是局部变量。

This is the code:这是代码:

public class App {
   public static void main(String[] args) {
       Animal fish = new Fish();
       ...
   }
}

I'm watching a video tutorial from Udemy and the instructor say that fish is an instance variable.我正在观看 Udemy 的视频教程,讲师说fish是一个实例变量。 I thought that a variable declared inside a method is a local variable.我认为在方法中声明的变量是局部变量。

Variables declared inside the class but outside the body of the method are called instance variables .在类内部但在方法体外部声明的变量称为实例变量 It is called instance variable because its value is instance specific and is not shared among instances.之所以称为实例变量,是因为它的值是特定于实例的,并且在实例之间不共享。

Variables declared inside the body of a method are called local variables .在方法体内声明的变量称为局部变量 You can use this variable only within that method and they are not seen outside of that method.您只能在该方法中使用此变量,并且在该方法之外看不到它们。

class A {
    int data = 50; //instance variable  
    void method() {
        int n = 90; //local variable  
    }

If you declare any variable within any method, it is a local variable.如果在任何方法中声明任何变量,它就是一个局部变量。 Your main might be a special method, but it is a method.您的 main 可能是一种特殊的方法,但它是一种方法。 Therefore, anything you declare in your main will be a local variable as well.因此,您在 main 中声明的任何内容也将是局部变量。

This is called by Scope .这由Scope调用。

Method body scope Variable is accessible in method body only (local variables, parameters)方法体作用域变量只能在方法体中访问(局部变量、参数)

Oracle defines "instance variable" thus: Oracle 定义了“实例变量”,因此:

Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword.实例变量(非静态字段) 从技术上讲,对象将其各自的状态存储在“非静态字段”中,即未使用 static 关键字声明的字段。 Non-static fields are also known as instance variables because their values are unique to each instance of a class非静态字段也称为实例变量,因为它们的值对于类的每个实例都是唯一的

I think the Udemy tutor is referring to "variables that reference an instance", using an unfortunate terminology.我认为 Udemy 导师使用了一个不幸的术语,指的是“引用实例的变量”。 In other words, he's using the term "instance variable" colloquially rather than with its official meaning.换句话说,他是通俗地使用术语“实例变量”而不是其官方含义。

It is local variable due to its visibility only in the main method whereas instance variables can been seen by all methods in the class.它是局部变量,因为它只在主方法中可见,而实例变量可以被类中的所有方法看到。

you may refer this old thread to tell the difference between local variables and instance variables您可以参考这个旧线程来说明局部变量和实例变量之间的区别

Your fish variable is a reference variable .您的fish变量是一个reference variable Reference variables are those variables which hold the memory reference to an Object (in your case, object of Fish class).引用变量是那些保存对对象(在您的情况下,是Fish类的对象)的内存引用的变量。 Instance variables are those variables which belong to an instance. Instance variables是属于一个实例的那些变量。 In your Fish class, you may have declared some variables that define the property of the object.在您的 Fish 类中,您可能已经声明了一些定义对象属性的变量。 Eg: color may be a variable that defines the color of a particular fish(an instance of Fish class).例如: color可能是一个变量,它定义了特定鱼的颜色(Fish 类的一个实例)。 Now for your answer, the fish variable is a local variable as it is present inside your main method which is just another method.现在为您解答, fish变量是一个局部变量,因为它存在于您的 main 方法中,而这只是另一种方法。 So, if you want to access the same object in some other method, you need to pass the reference of that particular object as an argument to that method like this:因此,如果您想在其他方法中访问同一个对象,则需要将该特定对象的引用作为参数传递给该方法,如下所示:

public class App {
   public static void main(String[] args) {
       Animal fish = new Fish();
       ...
       new App().anotherMethod(fish)    //pass the memory reference of the object
   }
  public void anotherMethod(Fish fishRef){  // memory reference is now in fishRef

      //fishRef now accesses the same object as your fish variable in main method
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM