简体   繁体   English

实例变量可以在类的底部声明吗?

[英]Can Instance variables be declared at the bottom of the Class?

I have a question when I saw "Instance variables can be declared in class level before or after use." 当我看到“实例变量可以在使用之前或之后在类级别声明”时,我遇到一个问题 in the site java_variable_types 在站点java_variable_types中

I don't understand what is the class level and the meanning of this sequense. 我不明白什么是课程级别以及该序列的含义。

I think they mean that this is legal: 我认为这是合法的:

 public class Test {
    private int someValue;

    public int myMethod() {
        return someValue + anotherValue;
    }

    private int anotherValue;
 }

(And it is!) (是!)

However, I think it is a mistake for the site to describe this as "[i]nstance variables can be declared in class level before or after use" . 但是,我认为该站点将其描述为“可以在使用之前或之后在类级别声明实例变量”是一个错误。

  1. The phrase "declared in class level" is bad English grammar. “在课堂上宣告”这句话是不好的英语语法。

  2. The phrase "in class level" is ambiguous. 短语“在课堂上”是不明确的。 It could mean declared in the body of a class. 它可能意味着在类的主体中声明。 However, it could also mean declared as "class-level" (ie static ) variables. 但是,它也可能意味着声明为“类级”(即static )变量。 (That is contradictory, and incorrect, but ...) (这是矛盾的,而且不正确,但是...)

  3. The phrase "before or after use" is ambiguous. 短语“使用之前或之后”是不明确的。 It could mean before or after in the source code file. 它可能意味着在源代码文件之前或之后。 It could also mean in before or after in the temporal sense. 在时间意义上,它也可能意味着之前或之后。 (That would be incorrect. At runtime, all of an object's instance variables are declared and initialized before the code in a method or constructor body is executed.) (那是不正确的。在运行时,在执行方法或构造函数主体中的代码之前,声明并初始化对象的所有实例变量。)


While what they are trying to say (I think) in that sentence is correct, they have expressed themselves poorly, and it is clearly causing confusion for some readers. 尽管他们试图说的(我认为)是正确的,但他们的表达能力很差,这显然使一些读者感到困惑。

Instance variable val is declared in line #2( please note the marking) but referenced even before in line #1. 实例变量val在第2行中声明(请注意标记),但甚至在第1行中也已引用。 You can remove comment from line #3 and comment line #2. 您可以从第3行和第2行注释中删除注释。 Then also it will work. 然后它也会起作用。

It means variable val using in line #1 even before declare in case if line#2 consider using after if you consider line#3. 这意味着变量val在第1行中使用,甚至在声明之前使用,如果第2行考虑在第3行考虑之后使用。

public class prog{
     //private  int val; //line# 3
      public  int getVal()
       { 
          return val;//line# 1
       }
        private  int val; //line# 2
        prog()
        {
           val=0;
        }
        public static void main( String [] args)
        {
            prog obj= new prog();
            System.out.println("val:"+obj.getVal());
        }
    }

While your reference does a good job of explaining it, I'll add some details for completeness. 尽管您的参考文献很好地解释了该问题,但为了完整性,我将添加一些细节。


An instance variable... 一个实例变量...

  • is declared at the class level 在班级声明
  • can have any visibility that is necessary ( public , protected , private , or no modifier to signify package-private) 可以具有必要的任何可见性( publicprotectedprivate或没有修饰符来表示package-private)
  • will receive an initial value after instantiation (that is, you don't have to instantiate the field with a value...but if you don't instantiate a reference value, you may run into a NullPointerException ) 将收到一个初始值实例化之后(也就是,你不必来实例字段的值...但是,如果你不实例化一个参考值,你可能会碰到一个NullPointerException

The phrase "before or after use" doesn't make much sense, but let's illustrate a scenario: 短语“使用前或使用后”没有多大意义,但让我们举例说明一种情况:

public class Foo {
    private String word;

    public void printTheWord() {
        System.out.println(word);
    }
}

word hasn't been instantiated, but we can use it since it's received an initial value of null . word尚未实例化,但是我们可以使用它,因为它收到的初始值为null This means that we won't get the value we want, but it will compile. 这意味着我们不会获得我们想要的值,但是编译。

Contrast this with a local variable. 将此与局部变量进行对比。 The below code won't compile because word hasn't been instantiated. 由于未实例化word因此以下代码无法编译。

public class Foo {
    public void printTheWord() {
        String word;
        System.out.println(word);
    }
}

The phrase "before or after use." 短语“使用之前或之后”。 would mean that the instance variable, which is applicable to the class as a whole ,unlike local variable which is restricted only inside of that method. 意味着实例变量(适用于整个类)与局部变量不同,局部变量仅在该方法内部受到限制。

It can be declared or initialized at the start of the class,which is generally the most likely way. 可以在类的开头声明或初始化它,这通常是最可能的方法。 Other , inside of the class, it can be declared , after the call of the method using it. 在类内部的other,可以在使用它的方法调用之后进行声明。

Please find the below code snippet to understand the phrase: 请找到以下代码片段以理解该短语:

public class InstanceVariable {

    //declared before
    int foo=4;
    public void testInstanceVariableUse(){
        System.out.println("The total value of the instance variable is "+ (foo+boo));
    }
    //declared after        
    int boo=5;
}


class TestInstanceVariable{
    public static void main(String[] args){
        InstanceVariable instanceVar = new InstanceVariable();
        instanceVar.testInstanceVariableUse();
    }
}

Output: The total value of the instance variable is 9 输出:实例变量的总值为9

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

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