0

According to this tutorial in the Java documentation, public members can be accessed on the Class, Package, Sublcass, and World levels. 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. Why, then, does the Java docs state that public access modifiers allow access everywhere? I understand that I am doing something wrong, but what exactly is going on?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • What if I also have another class with a field called `someNumber`. Which `someNumber` should `someNumber` refer to? Do you know what instance fields are? Do you know what class fields are? Do you know what scope is? (Those are all keywords to look up.) – Sotirios Delimanolis Sep 11 '15 at 00:20
  • Yeah, I'm familiar with fields (variables) and scope, I just didn't understand what I was doing wrong (not creating instances and trying to access the variables via dot notation). I see now. Thank you! Also, @RamanSB thank you as well, your examples helped. – Aleksandr Hovhannisyan Sep 11 '15 at 00:35
  • @AleksandrH, the table in the official tutorial isn't very pedagogical imo. Have a look at [this table](http://stackoverflow.com/a/33627846/276052). Even though it doesn't address your exact question, it clarifies the scopes a bit better. – aioobe Nov 13 '15 at 12:50

2 Answers2

3

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.

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.

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. The count variable would be inaccessible to any other class except the class that declares it.


To expand upon Sotirios Delimanolis's comment, consider the following Scenario:

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? You cannot say. Hence the variables are accessed via a reference variable, the reference class indicates which fields can be accessed. I.e.

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

    CounterExampleClass cRef = new CounterExampleClass();
    cRef.someNumber; //3
RamanSB
  • 1,162
  • 9
  • 26
1

The variable you referencing is bound to bring errors as it is not recongised in that class, create first an instance of TesterClass

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

Never use variables you havent declared!!...Happy coding

AguThadeus
  • 310
  • 2
  • 10