Reputation: 137
I have main class with a private static method. I want to access this method from another java class. I tried some ways,however they didnt work. How can I access the method?
below main class like this;
public class RandomGenerate {
public static void main(String[] args) throws Exception {
System.out.print.ln("main method");
}
private static synchronized void createRandom(PersonObj person, int number, List s) {
System.out.println("deneme");
}
}
And I want to call createRandom
from another java class like this;
public class Deneme {
RandomGenerate rg = new RandomGenerate();
RandomGenerate.createRandom(person, number, sList);
}
Then, netbeans shows method has private access.
Upvotes: 2
Views: 13825
Reputation: 364
Just change the visibility from private
to public
so other Instances can access them. Private means it is only for the own class available.
Upvotes: 0
Reputation: 559
You can not access Private methods outside the class which defines this method. You should make it Public to give full access to any classes or protected to give access to all the classes in the same package.
Click [here] http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html For more reference.
If you really wish to access private method, you will have to use Java Reflection. See this sample code.
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Workspace {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ClassWithPrivateMethod cwpm = new ClassWithPrivateMethod();
Method m = cwpm.getClass().getDeclaredMethod("privateMethod", String.class);
m.setAccessible(true); //This is a key statement for accessing private methods
m.invoke(cwpm, "test");
}
}
class ClassWithPrivateMethod {
private void privateMethod(String someParam){
System.out.println("I am private!!!");
System.out.println("Parameter: " + someParam);
}
}
This code will print following output:
I am private!!!
Parameter: test
Upvotes: 0
Reputation: 1075567
If you need to use it outside the class, make it public
(or protected
if you need it only in subclasses, or the default [no keyword at all] if you need it just in the package). If you need to use it outside the class and it's private
and you can't make it not private
, that's a design problem you should fix.
...you can work around it using reflection (tutorial, docs), which allows you to get the method and call it even though it's private. Once you have the Method
object, you have to call setAccessible
to true
before you call it.
But again, that's a workaround. Use the correct access modifier.
Upvotes: 7
Reputation: 2037
You shouldn't access a private
function/variable from outside of that class. If you need to access a private
variable of a class, you can create an accompanying getter
for that variable, and call the getter
function on the class.
For functions, if the class you are trying to access the function from is in the same package, or is a subclass as the class with the function, change private
to protected
. protected
allows members in the same package, or subclasses, to access the item, but nothing outside of the package.
A good read on visibility in Java
is: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
That shows a table:
Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Upvotes: 10
Reputation: 2789
You will want to choose the proper access modifier for the method, the options are: public
, protected
, default
(which is indicated by not providing a modifier), and private
.
A good explanation is here: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
As mentioned in the comments, you can use public
to open it up, but if you don't want such a wide access, you could start with default
(which allows you to access the method if you're in the same package), or protected
(which is the same as default, but also allows child classes to access the method, if you wanted to extend the class).
As a general rule, stick with the most restrictive permission. It's easier to open up permissions later, but very hard to remove them.
Upvotes: 0
Reputation: 418
private
methods are not accessible from another class by definition. If you need to call it you can create another public method that internally calls the private one or change the access modifier to public/protected/default.Example:
private static String secretMethod() { return "secret"; }
public static String knownMethod() { return secretMethod(); }
Upvotes: 3