Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a private method in Java

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)
}
like image 331
Dan Avatar asked Oct 08 '11 05:10

Dan


2 Answers

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.

like image 167
Ray Toal Avatar answered Oct 27 '22 22:10

Ray Toal


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.

like image 25
brc Avatar answered Oct 27 '22 21:10

brc