Java static reference vs dynamic reference and calls -
here post 2 simplified versions of code (but self sufficient, don't need know other variables , on).
i need class including problem import class synchronous_writing package. need class static various classes must call , modify object created @ first subsequently, , not option pass data 1 class make code unreadable...
this class public, still access current class involve problem described below version below. advance help.
backbone of synchronous_writing:
public final synchronous_writing { public synchronous_writing() { // variable initialize } public static void writer () { //operation performed } }
1st version:
import synchronous_writing; public classfiletoedit_parser { // public static final class synchronous_writing syncedwriter ; // note editor; ";", not { public filetoedit_parser ( string inputfile_filetoedit, string outputfile_editedfile, charset charset, string hitlist_fieldsseparator, string commentline) throws ioexception, filenotfoundexception { syncedwriter = new synchronous_writing( outputfile_editedfile, charset, standardopenoption.append); } protected void parser_writer(path filetoedit) throws ioexception, filenotfoundexception { syncedwriter.writer(); } }
//the problem here compiler says "the static method writer(string) type synchronous_writing should accessed in static way" (logical since synchronous_writing static)
2nd version:
import synchronous_writing; // public static final class public classfiletoedit_parser { public filetoedit_parser ( string inputfile_filetoedit, string outputfile_editedfile, charset charset, string hitlist_fieldsseparator, string commentline) throws ioexception, filenotfoundexception { syncedwriter = new synchronous_writing( outputfile_editedfile, charset, standardopenoption.append); } protected void parser_writer(path filetoedit) throws ioexception, filenotfoundexception { // syncedwriter.writer () // won't work, don't understand why since // constructor public, still can't access synchronous_writing.writer () // work, compiler keeps saying syncedwriter // variable not used (which quite true, since // constructor initialized) } }
to correct 1st version's problem, suppressed synchronous_writing syncedwriter; in class declaration , switched variable call through class , not object. suppressing synchronous_writing syncedwriter; not mandatory, still, doesn't change problem.
my second problem syncedwriter.writer () won't work, don't understand why since constructor public, still can't access it. synchronous_writing.writer () work, compiler keeps saying syncedwriter variable not used (which quite true, since constructor initialized)
in end, both codes botched in way, , know properly, please feel free give insights.
static
in java means, method not know context except passed parameters , other static variables.
you trying create context in synchronous_writing
, call static method on context. calling method without step initialize context in synchronous_writing
case exception. bad practice , not self-explanatory code.
if need static method, make sure doesn't depend on context needs initialized.
Comments
Post a Comment