Java/common function for multiple projects
Hi Dear Henry
I am a newbie to java programming.
I have three independent projects and i need to write a common function for all of them.
Now i write in in one project and import to another.Is there better way?
What is the best way for doing that?
Thanks in advance for your answer.
Best regards ,
Hello Dear Sara,
The way you have done it(importing the function from one project is a better way) for a beginner like you but there is a much more better way. Since this function is so important, think of what will happen if you keep having more and more projects that will depend on that function..Now, suppose you have have projects A, B, C, D, and E all related and the function(importantFunc()) is in project A that is fine but what happens if you have project X which is not so related to the previous projects but have to make use of the function importantFunc() in A, do you just import it into X even though X and A are unrelated? That is when another way becomes useful. That way is called interface. why not write an interface with that function and let any class that needs it implement that interface? that is perhaps a better way than putting the function in one specific funtion. any project that needs the function should just implement itz own version of the function :)
have a blast..