Search code examples
javacode-organization

How to properly organize class code?


What is the preferred way to organize the code in a class if you need to modify the data via a private method?

For example, is it better to do this:

private String data1;

callMethod(data1);

private void callMethod(String stuff) { 
    // Do stuff to data1
}

Or this:

private String data1;

callMethod();

private void callMethod() { 
    // Do stuff to data1
}

I've seen it done in a variety of ways and I'm trying to understand what is an industry standard best practice since I'm new to the developing world.


Solution

  • If the data is private to the object, the method has full access to it. You should not have to pass it in.

    Objects encapsulate state and behavior into a single software module. The operations should manipulate the state.