5

I am given a class that has a private method say setCoors(int x, int y). The constructor of that class has the setCoors in it too. In a different class, I want to have a method setLocation which calls setCoors. Is this possible?

New Question:

If I am not allowed to set the method to public, is this possible?

public class Coordinate{
    public Coordinate(int a, int b){
        setCoors(a,b)
    }
    private void setCoords(int x, int y)
}

public class Location{
    private Coordinate  loc;
    public void setLocation(int a, int b)
        loc = new Coordinate(a,b)
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Dan
  • 8,263
  • 16
  • 51
  • 53
  • 3
    @RayToal - you're making some big assumptions. Given that he doesn't understand the whole public vs. private thing, I doubt his class has quite made it to reflection yet. Of course, that's an assumption too ... just a slightly smaller one given that he's stating the constructor calls the private method ... ;) – Brian Roach Oct 08 '11 at 05:44
  • @Brian I do agree that yes, the answer of "no" is more helpful because you're right the question _is_ coming from a beginning Java context. I'm not opposed to leaving the answer in place for other visitors to the page, but if there is consensus that it does not belong I can withdraw it. Still, I can't decide, though, whether the _best_ answer is a qualified no; something like "well no you can't but weeeeellllll, there _is_ this really complicated back door that lets you do it but no one does this except these evil hackers and you can learn about it later...." Or maybe not. :) – Ray Toal Oct 08 '11 at 06:05

6 Answers6

7

The best and most helpful answer depends on the context of the question, which is, I believe, not completely obvious.

If the question was a novice question about the intended meaning of private, then the answer "no" is completely appropriate. That is:

  • private members of A are accessible only within class A
  • package-private members of A are accessible only within classes in A's package
  • protected members of A are accessible only within classes in A's package and subclasses of A
  • public members of A are accessible anywhere A is visible.

Now, if, and okay maybe this is a stretch (thank you Brian :) ), that the question came from a more "advanced" context where one is looking at the question of "I know private means private but is there a language loophole", then, well, there is such a loophole. It goes like this:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

class C {
    private int x = 10;
    private void hello() {System.out.println("Well hello there");}
}

public class PrivateAccessDemo {
    public static void main(String[] args) throws Exception {
        C c = new C();
        List<Field> fields = Arrays.asList(C.class.getDeclaredFields());
        for (Field f: fields) {
            f.setAccessible(true);
            System.out.println(f.getName() + " = " + f.get(c));
        }
        List<Method> methods = Arrays.asList(C.class.getDeclaredMethods());
        for (Method m: methods) {
            m.setAccessible(true);
            m.invoke(c);
        }
    }
}

Output:

x = 10
Well hello there

Of course, this really isn't something that application programmers would ever do. But the fact that such a thing can be done is worthwhile to know, and not something that should be ignored. IMHO anyway.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Seriously, you're going to "help" a student that doesn't understand public vs. private with reflection? – Brian Roach Oct 08 '11 at 05:42
  • haha too funny. Ray is right, but I think Brian is even more right. ;) "No" is a much better answer. :) – Eric Lindauer Oct 08 '11 at 05:46
  • Actually there is no way to tell from the question that the student does not understand public versus private. This could have been a research question in an advanced class. But your question is fair. I will qualify the answer, thanks. – Ray Toal Oct 08 '11 at 05:47
5

No, private means the method can only be called inside of the Class in which it is defined. You will probably want to have setLocation create a new instance of the class setCoords resides in, or change the visibility on setCoords.

EDIT: The code you have posted will work. Just be aware that any instance of the Location class will be bound to its own Coordinate object. If you create a new Coordinate object somewhere else in your code, you will be unable to modify its internal state. In other words, the line

Coordinate myCoord = new Coordinate(4, 5);

will create the object myCoord which will forever have the coordinates 4 and 5.

brc
  • 5,281
  • 2
  • 29
  • 30
  • So if I'm not allowed to make setCoords public. then in setLocation, do I do something like this: setLocation(int a, int b) { coords = new Coordinate(a,b)? assuming setCoords in is Coordinate class – Dan Oct 08 '11 at 05:37
  • Yes, assuming that is what you want. Without seeing the class design, it's hard to tell if that is the right way to approach the problem. – brc Oct 08 '11 at 06:07
4

private means it's private

If you want other classes to call it, maybe you shouldn't make it private?

  • As @Dan said in his reply to my answer, he can't change the visibility of the `setCoords` method. – brc Oct 08 '11 at 06:09
  • Not true. It can be accessed with reflection. He didn't ask if he should, he asked if he could. Now I fully agree that you should tell him he shouldn't, but that doesn't change the fact that the correct answer is "Yes, but ..." – CorayThan Jun 04 '13 at 17:18
3

No private methods can't be accessed outside the class in which they are defined

shubhendu mahajan
  • 816
  • 1
  • 7
  • 16
3

Kid-doing-homework: the answer is no. Guy-requiring-some-crazy-work-around-for-his-job: the answer is yes. Far more importantly though, Your setCoors method should not take int arguments. It should take two SilverBullet objects.

Eric Lindauer
  • 1,813
  • 13
  • 19
1

private means you can only access it inside the class defined.

Jay
  • 520
  • 2
  • 9
  • 23