How to use a private method in Java

I am assigned a class that has a private method: setCoors (int x, int y). The constructor of this class also has setCoors in it. In another class, I want to have a setLocation method that calls setCoors. Is it possible?

New question:

If I am not allowed to publish this method, 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) } 
+4
source share
6 answers

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

EDIT: The code you posted will work. Just keep in mind 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 cannot change its internal state. In other words, the line

 Coordinate myCoord = new Coordinate(4, 5); 

will create an object myCoord , which will always have coordinates 4 and 5 .

+4
source

private means private

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

+4
source

The best and most useful answer depends on the context of the question, which, in my opinion, is not completely obvious.

If the question was a beginner, the question of the alleged meaning of the particular, then the answer "no" is completely appropriate. I.e:

  • private members A are only available in class A
  • Package-private members of A are only available inside classes in package A
  • protected members of A are only available in classes in package A and subclasses A
  • public members of A are available anywhere in A that is visible.

Now, if, and maybe, it can be extended (thanks to Brian :)) that the question came from a more "advanced" context, which addresses the question "I know that private funds are private, but there is a language loophole", then Well, there ’s such a loophole. This happens as follows:

 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 is really not what application programmers have ever done. But the fact that such a thing can be done is worth knowing, and not something that should be ignored. IMHO in any case.

+4
source

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

+3
source

Kid-do-homework: the answer is no. Guy-demanding-crazy-work-around-for-his-job: the answer is yes. Especially since your setCoors method setCoors not accept int arguments. It should take two SilverBullet .

+2
source

private means that you can only access it inside a specific class.

+1
source

All Articles