简体   繁体   English

Java:访问修饰符混乱

[英]Java: Access Modifier Confusion

According to this tutorial in the Java documentation, public members can be accessed on the Class, Package, Sublcass, and World levels. 根据Java文档中的教程,可以在类,包,Sublcass和World级别上访问公共成员。 But say I create two classes like so: 但是说我创建两个这样的类:

public class TesterClass
{
    public int someNumber = 5;
}

public class AnotherClass
{
    public static void main( String [] args )
    {
         System.out.println( someNumber );
    }
}

and save them in the same location. 并将它们保存在同一位置。 When AnotherClass is compiled, an error is thrown saying that the variable someNumber cannot be recognized. 编译AnotherClass ,将引发错误,指出无法识别变量someNumber Why, then, does the Java docs state that public access modifiers allow access everywhere? 那么,为什么Java文档会声明公共访问修饰符允许到处访问? I understand that I am doing something wrong, but what exactly is going on? 我知道我做错了什么,但是到底是怎么回事?

Those two classes are not related in any way, shape, or form. 这两类没有任何形式,形状或形式的关联。

You need to create an instance of the TesterClass in AnotherClass , then access the variable via the reference. 您需要创建的实例TesterClassAnotherClass ,然后通过参考访问变量。

public class AnotherClass
{
    public static void main( String [] args )
    {
         TesterClass classRef = new TesterClass();
         System.out.println(classRef.someNumber);
    }
}

This will work and produce an output of 5. 这将起作用并产生5的输出。

However, if we changed the access modifier of the count variable from public to private and then attempted to do the same thing, this would no longer work. 但是,如果我们将count变量的access修饰符从public更改为private ,然后尝试执行相同的操作,那么它将不再起作用。 The count variable would be inaccessible to any other class except the class that declares it. 除了声明它的类之外,任何其他类都无法访问count变量。


To expand upon Sotirios Delimanolis's comment, consider the following Scenario: 为了扩展Sotirios Delimanolis的评论,请考虑以下情形:

public class TesterClass
    {
        public int someNumber = 5;
    }

    public class CounterExampleClass
    {
        public int someNumber = 3;
    }

    public class AnotherClass
    {
        public static void main( String [] args )
        {
             System.out.println( someNumber );
        }
    }

According to your logic, what would be printed 3 or 5? 根据您的逻辑,将打印3或5? You cannot say. 你不能说。 Hence the variables are accessed via a reference variable, the reference class indicates which fields can be accessed. 因此,变量是通过参考变量访问的,参考类指示可以访问哪些字段。 Ie

    TesterClass tRef = new TesterClass();
    tRef.someNumber; //5

    CounterExampleClass cRef = new CounterExampleClass();
    cRef.someNumber; //3

The variable you referencing is bound to bring errors as it is not recongised in that class, create first an instance of TesterClass 您引用的变量必然会带来错误,因为在该类中未将其重新识别,请首先创建TesterClass的实例

    TesterClass testObj=new TesterClass();
//then call println 
System.out.println(testObj.someNumber);

Never use variables you havent declared!!...Happy coding 永远不要使用您尚未声明的变量!

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

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