1

When designing test or implementation code for my Classes, I like to split the code into methods for readability and functionality. However, given that main() is static, that makes passing data and control back and forth from a static method to non-static method a pain.

What I thought of doing is either leaving main() blank with one method call to my actual implementation Class (which the code is non-static), or making everything in my implementation Class private static

Are there any problems with either? I'm leaning towards making everything private static.

Lightfire228
  • 546
  • 1
  • 5
  • 17

3 Answers3

3

making everything in my implementation Class private static

This is contradicting the OOP way, if your project needs only funcions and procedures and does not rely on objects and messages, it is a clear indication that something in your design is very wrong when it comes to Object Oriented Programming.

Having static methods is bad when it comes to OOP design becasue it does not allow you to override them and use the important dynamic dispatch property, and it allows much less reusability of your code.

Your way of thinking seems to be of an imperative language programmer (C?), if you truly want to be a better java programmer, you should utilize a better OOP design. Make sure you have classes that interact with each other and send messages (invoke methods) on each other in an orginized way.

Try and see OOP tutorials, try to think how you can make your project more object oriented. It might be hard and seems redundant at first, but once you get used to it - you will be able to unlock one of java's most important aspects - being OOP language, and you won't "program C in java".

amit
  • 175,853
  • 27
  • 231
  • 333
0

You could try calling non static method something like below:

public static void main(..) {
   ..
   MyClass clazz = new ...;//create object
   clazz.myMethod();//call method on object itself rather than making all methods static.
}
SMA
  • 36,381
  • 8
  • 49
  • 73
0

I understand your doubts about the mix-up and I used in the past this pattern to avoid static methods at all and reduce complexity of used language features.

public class Main {
    public static void main(String[] args) {
        new Main().m(args);    
    }
}

But in Java static methods are an important design element (Java: when to use static methods). And you will find static methods all around in the API's. So you should use them as expected too.

Community
  • 1
  • 1
PeterMmm
  • 24,152
  • 13
  • 73
  • 111