为什么基类中的静态变量在子类中无法访问?

huangapple 未分类评论50阅读模式
英文:

Why is the static variable in Base Class not accessible in Child Class?

问题

class BaseClass{
    static int count1=10;
    public void display(){
        System.out.println("Invoking display method of Base Class");
    }
}

public class SingleInheritance01 extends BaseClass {
    int value=200;
    static int count2=10;
    public static void main(String args[]){
        SingleInheritance01 objDerivedClass=new SingleInheritance01();
        objDerivedClass.display();
    }
}

/*
The object created in Derived Class cannot reference static variables in Base Class as well as 
Derived
I can access it using ClassName -I am under the assumption that as static variables are common to instances of the class.
*/
英文:
class BaseClass{
    static int count1=10;
    public void display(){
        System.out.println("Invoking display method of Base Class");
    }
}

public class SingleInheritance01 extends BaseClass {
    int value=200;
    static int count2=10;
    public static void main(String args[]){
        SingleInheritance01 objDerivedClass=new SingleInheritance01();
        objDerivedClass.display();
    }
}

/*
The object created in Derived Class cannot reference static variables in Base Class as well as 

Derived
I can access it using ClassName -I am under the assumption that as static variables are common to instances of the class.
*/

答案1

得分: 0

首先,静态变量与实例无关。应该使用类名来访问它们。你可以使用对象来访问它,但这并不推荐。如果将 count1 设为 static 是为了在子类中访问它,你可以移除 static 修饰符。
其次,count1 的可见性取决于访问修饰符。现在它是 default,这意味着它只对同一包中的类可见。所以我猜想你的 BaseClass 与子类位于不同的包中。

如果你将其改为 protectedpublic,你就能在子类中访问它。

如果你确实想将其保持为静态,那么可以这样做,但必须将其访问权限改为 protectedpublic

英文:

First off, static variables are not tied to instances. You should be accessing them using Class name. You could use the object to access it, but its not recommended. If the purpose of making count1 static was to access it in a sub class, you can remove the static modifier.
Second, the visibility of count1 is determined by the access modifier. Right now its default. Which means its visible only to classes in the same package. So my guess is that your BaseClass is in a different package than your subclass.

If you change it to protected or public you'll be able to access it in the sub class.

If you do want to keep it static, then you may do so but will have to change its access to protected or public

答案2

得分: 0

静态变量在定义上不属于“实例变量”。它们的状态对该类的所有实例都是可用的。

它们被称为类变量,通常用于保存在类的所有实例之间共享的资源。当然,在某些情况下,您必须考虑线程问题。

类变量(静态变量)的一种用途是实现“单例设计模式”。

您可以在同一包中的任何类中访问基类的静态变量(在您的情况下是默认包,因为未定义任何包)。这包括包内的任何派生类。如果要在不同包之间共享变量,请使用“public”修饰符。要访问静态变量,只需在变量名前加上类名。

例如:BaseClass.count1

此外,请注意,在使用静态变量时,它们不会在派生类中继承。

英文:

Static variables are by definition not "instance variables". Their state is available to all instances of that Class.

They are termed as class variables and are generally used to hold resources shared across the instances of the class. Of course you would have to take into account threading issues in certain situations.

One use of a class variable (statics) is in an implementation of the Singleton Design Pattern.

You could access static variables of the base class in any of the classes in the same package (default package in your case since no packages are defined). This includes any of the derived classes within the package. If you want to make it available across packages, use the "public" modifier. To access a static variables, simply prefix the class name.

Ex: BaseClass.count1

Also note when using static variables, they are not inherited in derived classes.

huangapple
  • 本文由 发表于 2020年4月10日 16:31:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61136608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定