Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There's a developer I've worked with for over 20 years, she's great, she's smart, and we complement our respective strengths & weaknesses.

We're both learning java, I'm perhaps a bit ahead, but we both kinda suck. Nevertheless, we have coded up a system, which is at the point where it's being demo'ed to clients.

Thing is, just about everything she's built is using static classes. I feel like that's bad practice, but I don't know for sure. It does all work. I'm not sure if her stuff is ever going to be multithreaded, if so it can't be static yes? Well ignore that for now.

What I have tried:

Eventually I need to understand her stuff, but for now I really don't. Except it did start crashing on close, but in fairness, that was caused by my changes to logging. So I tried to figure out the heirarchy of her classes, so I could check each level cleans up everything it created when it closes. Dear god, it's all over. I don't know if there are circular references, but there may be.

Standard classes have clear instantiate and destroy points, you build towers, you tear them down. Her stuff, to be clear most classes contain at least one data class, which get populated and destroyed in specific methods, then accessed statically. I would absolutely have used extends AutoCloseable, and made nothing static. Which is preferred? Are both OK? Are neither?

EDIT I do use static, but only for functions that accept inputs, return a result, and then forget everything. Oh. and a class that reads an xml file at start, closes it, thereafter provides system settings statically.

EDIT 2: Thank you for your answer, apologies, I did not mean the class itself was static, the data objects and related methods are. Here's a simplified block of code:

public class Data {
	private static parm_autoytdCollection colParmAutoYTD;


	public static void initialise() throws HandledException {
		colParmAutoYTD = new parm_autoytdCollection(GlobalData.dataConnect);
		colParmAutoYTD.selectData();
	}


	public static void close() {
		clsLogger.closeQuietly(colParmAutoYTD);			
	}
	
	
	public static void updateYTD() throws HandledException{
		for (parm_autoytdRecord YTD :colParmAutoYTD.dataList)
		{
			source = Tasks.getYTD(sourceOPA, YTD.sourceCodeN.getValue());
			destination = Tasks.getYTD(destOpA, YTD.destinationCodeN.getValue());
			Tasks.setYTD(destOpAUpd, YTD.destinationCodeN.getValue(), source + destination);
		}
	}
	

	public static void SaveReportTotals() throws HandledException {
		colParmAutoYTD.saveChanges();
	}
}


Also, again, thank you for the link, but I'm afraid the official documentation is normally quite understated, whereas I need something along the lines of IF YOU DO THIS, Larry Ellison will KILL A PUPPY.
Posted
Updated 21-Nov-22 0:35am
v4

1 solution

the only time you should use static on a class definition is Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900