java - Do you think I'm abusing of statics? -
i'm new in java programming , think have clear objects , how work them.
however, i'm writing program have noticed have used lot 'static' keyword methods, , i'm doubting if because neccesary , logical or if because have not internalized in mind oo concepts.
to more specific, program should read txt file , put each line in arraylist, code:
public class filebody { private static final string separator = ";"; private static string headerfield1 = "regex"; private static string headerfield2 = "origin"; private static string headerfield3 = "destination"; private static final string header = headerfield1 + separator + headerfield2 + separator + headerfield3 + separator; // getters & setters public static string getheader() { return header; } public static string getheaderfield1() { return headerfield1; } public static void setheaderfield1(string headerfield1) { filebody.headerfield1 = headerfield1; } public static string getheaderfield2() { return headerfield2; } public static void setheaderfield2(string headerfield2) { filebody.headerfield2 = headerfield2; } public static string getheaderfield3() { return headerfield3; } public static void setheaderfield3(string headerfield3) { filebody.headerfield3 = headerfield3; } // end getters & setters public static file createfileifnotexists(string path) throws ioexception { file file = new file(path); if (file.createnewfile()); return file; } public static file getfile(string path) throws ioexception { file file = createfileifnotexists(path); return file; } public static boolean isempty(file file) throws exception { filereader filereader = new filereader(file); if (filereader.read() != -1) { filereader.close(); return false; } else { filereader.close(); return true; } } public static void writeheadertoemptyfile(file file) throws exception { if (isempty(file)) { bufferedwriter bufferedwriter = new bufferedwriter( new filewriter(file, false)); bufferedwriter.write(header); bufferedwriter.close(); } else { return; } } public static arraylist<string> getlines(file file) throws exception { arraylist<string> lines = new arraylist<>(); bufferedreader bufferedreader = new bufferedreader(new filereader(file)); while (bufferedreader.ready()) { lines.add(bufferedreader.readline()); } bufferedreader.close(); return lines; } } do think have done better objects? if answer yes, give me guidelines that?
thank help.
having mutable static fields should avoided where-ever possible. in particular, have won't work because initalised once.
// run once if these fields changed. private static final string header = headerfield1 + separator + headerfield2 + separator + headerfield3 + separator; most want is
public static string getheader() { return headerfield1 + separator + headerfield2 + separator + headerfield3 + separator; } the field should static separator constant. try make other fields non-static fields (and getter/setters)
you have utility/helper methods @ end of class. put these in class don't appear related. i.e. have clear utility class these methods instead. e.g.
class filebody { public void writeheadertoemptyfile(file file) throws ioexception { if (!fileutils.isempty(file)) return try (writer w = new filewriter(file)) { w.write(getheader()); } } } class enum fileutils { /* no instances */ ; // todo replace callers new file(x); public static file getfile(string filename) { return new file(filename); } public static boolean isempty(file file) { return file.length() > 0; } public static list<string> getlines(file file) throws exception { return files.readalllines(paths.get(file.getabsolutepath())); } }
Comments
Post a Comment