Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 派生类中的最终变量初始化_Java_Final - Fatal编程技术网

Java 派生类中的最终变量初始化

Java 派生类中的最终变量初始化,java,final,Java,Final,如果我们可以在基类的派生类中初始化基类的最后一个变量,我会感到困惑。 我的基础课是 abstract class MyClass1 { //Compiler Error:Variable is not initialized in the default constructor. public final int finalVar; } //And my derived class with variable initialization

如果我们可以在基类的派生类中初始化基类的最后一个变量,我会感到困惑。 我的基础课是

    abstract class MyClass1 {
        //Compiler Error:Variable is not initialized in the default constructor.

        public final int finalVar;
    }
    //And my derived class with variable initialization.

    class DerivedClass extends MyClass1 {
        DerivedClass()
        {
            super();
            //Cannot asssign a value to finalVar.
            finalVar = 1000;
        }
   }

请告诉我是否可以初始化派生类中的最终变量。它只会产生编译时错误还是运行时错误?

您必须为父类中的变量赋值

// Declaring Parent class
class Parent {
    /* Creation of final variable pa of string type i.e 
    the value of this variable is fixed throughout all 
    the derived classes or not overidden*/
    final String pa = "Hello , We are in parent class variable";
}
// Declaring Child class by extending Parent class
class Child extends Parent {
    /* Creation of variable ch of string type i.e 
    the value of this variable is not fixed throughout all 
    the derived classes or overidden*/
    String ch = "Hello , We are in child class variable";
}

class Test {
    public static void main(String[] args) {
        // Creation of Parent class object
        Parent p = new Parent();
        // Calling a variable pa by parent object 
        System.out.println(p.pa);
        // Creation of Child class object
        Child c = new Child();

        // Calling a variable ch by Child object 
        System.out.println(c.ch);
        // Calling a variable pa by Child object 
        System.out.println(c.pa);
    }
}

最终成员需要在类的构造函数中初始化。您可以修改代码,使子类通过构造函数传递值:

abstract class MyClass1 {

    public final int finalVar;

    protected MyClass1(int var) {
         finalVar = var;
    }

} 

class DerivedClass extends MyClass1 {
    DerivedClass() {
        super(1000);
    }
}

最后一个变量需要在构造函数中初始化,这是抽象类所缺少的。添加该构造函数之后,您可以通过super()调用它。考虑下面的例子:

public class Main {
    public static void main(String[] args) {
        baseClass instance = new derivedClass(1);
    }
}

abstract class baseClass {
    protected final int finalVar;
    baseClass(int finalVar){
        this.finalVar = finalVar;
    }
}

class derivedClass extends baseClass {
    derivedClass(int finalVar){
        super(finalVar);
        System.out.println(this.finalVar);
    }
}

它只是给出编译时错误还是也会给出运行时错误。
-如果您的代码没有通过编译,您就无法运行它,因此该代码将没有运行时。您应该参考最终的变量声明文档!编译错误没有说明最终字段必须由基类构造函数初始化吗?为什么不运行它并自己找到答案?