5

So right now I have the following standard package name setup in my project:

SampleClass.java looks like this:

package main.java.model;

public class SampleClass {
    int packagePrivateMethod() {
    return -1;
    }
}

And SampleClassTest.java looks like this:

package test.java.model;

import main.java.model.SampleClass;

public class SampleClassTest extends junit.framework.TestCase {
    private SampleClass sampleClass;

    public void setUp() {
    this.sampleClass = new SampleClass();
    }

    public void test_packagePrivateMethod() {
    // this method can't be called right now why?
    //this.sampleClass.packagePrivateMethod();
    }
}

Why can't the method packagePrivateMethod() be called???

2
  • its not private. It has default access. Apr 21, 2014 at 5:06
  • You do know that package private (default) access means that only other classes in the same package can access it?
    – Raedwald
    Apr 24, 2014 at 11:55

1 Answer 1

5

It can't be called because the two classes are not in the same package. One is in main.java.model the other in test.java.model.

2
  • 2
    But what about the best answer here: stackoverflow.com/questions/3004710/…
    – letter Q
    Apr 21, 2014 at 16:49
  • In this answer (i.e. a conventional maven project setup), two source folders are used src/main/java and src/test/java. The two classes are therefore in the same package (in this case the root or default package).
    – Henry
    Apr 23, 2014 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.