Java Method Design query
This is a fairly rudimentary question but one that I am kind of on the
fence about. Lets say I have a class A and it has methods
method1,method2,method3,method4 and a main method.
method2 is only invoked by method1; method4 is only invoked by method3.
The solution says to invoke the method1 from main and also method2 from
main and same with method3 and 4.
So isn't it bad design to have the main method invoke method1, and method2
explicitly? What is the point of having private methods in a class if you
invoke them in the main method even though they are only dependent on a
single method in the whole class?
Wouldn't it be cleaner to call method2 from method1 and method4 from
method3 since in both cases the latter method is only invoked by the
former?
I thought this was the whole point of helper methods, so that we are able
to abstract away unnecessary details of the implementation.
Again my apologies for the simplicity of the question, I am quite new to
java.
Class A{
public static void main(String[] args){
int x = method1()
if ( x = 0){
//user wants to create a new account
method2()
}
}
private static int method1(){
//some code to check user login credentials in list of users
//if login credentials fail,user is asked if they want to create a new
account, if yes,
//method 2 is invoked
//return value is whether the user wants to create a new account or not.
}
private static void method2(){
//creates new account for user and is only invoked by method1.
}
}
In the above case wouldn't it just be easier to call method2() from
method1() instead of calling it in the main(). I would like to know if
there are any advantages or disadvantages of this style of implementation.
No comments:
Post a Comment