Reputation: 25
I'm trying to use a method from another class, but I think I can't really use the constructor here is the first class :
public class Rules {
public Rules(int size) {
//body
}
public void methodINeed() {
}
}
and I want to use the method in it in my second class, but since if I use the constructor I have to give an int, which basically screws up my calculations, i'm left with no idea of what to do, what are my possibilities here?
Upvotes: 0
Views: 10184
Reputation: 2473
I think you have to review the design of your classes why in the earth you have to call a method in other class for calculations purpose ???
possible solution:
Upvotes: 0
Reputation: 6638
just make another empty constructor:
public class Rules{
public Rules(int size){
//body
}
public Rules()
{
//body
}
public void methodIneed(){
}
}
Then to access the method you need,
Rules x = new Rules();
x.methodINeed();
Upvotes: 1
Reputation: 57
You can access methods of other classes without contructing them if you declare those methods static:
public class Rules{
public Rules(int size){
//body
}
public static void methodIneed(){
}
}
Upvotes: 0