开发者

Exposing objects defined in a method to another method

开发者 https://www.devze.com 2023-03-03 13:58 出处:网络
I need to \"expose\" (I can\'t think of a better term) a set of objects (file channels, and bytebuffers) that are built in one method, but need to be accessed from another method. Tried using the pub

I need to "expose" (I can't think of a better term) a set of objects (file channels, and bytebuffers) that are built in one method, but need to be accessed from another method. Tried using the public static accessors, but that only generated compiler errors. I have the "main" method e.g.

public static void main(String args[]) throws IOException, FileNotFoundException
{ 
      ConnectFiles()  ; // builds the filestream, channel and bytebuffer objects
      ProcessRqst()   ; // reads the request file and writes to the response file
}   // end of main routine 

public static void ConnectFiles throws IOException 
{ 
     FileInputStream RqstInp = new FileInputStream(Rqst_File) ; 
     FileChannel RqstChnl  = RqstInp.getChannel() ; 
     ByteBuffer  Rqst_Bufr = ByteBuffer.allocate(RQbuflen) ; 

     //Connect to the Response Output File 
     FileOutputStream RespOut  = new FileOutputStream(Resp_File) ;
     FileChannel RespChnl =  RespOut.getChannel();
     ByteBuffer  Resp_Bufr = ByteBuffer.allocate(RSbuflen) ;
}   // end of connect-files routine 

public static void ProcessRqst() 
{  
    while ( pgm_status != EOF ) 
    {  
        try {
            RqstChnl.read( Rqst_Bufr ) ;    
            Rqst_Bufr.get( RqstWork, bufroffset, RQbuflen ) ;
        }  
        catch ( EOFException e ) 
        { 
            DT_Stamp = getDateTime() ;
            message_data.append(DT_Stamp) ;
            message_data.append(" LU62XCE0599: Request File at End-Of-File ") ;
            message_data.append(" Request File:") ; 
            message_data.append(Rqst_File) ;
            SendMesg() ; 
            pgm_status  = EOF ;    //set program status to End-Of-File 
        }
        catch ( IOException e ) 
        { 
            DT_Stamp = getDateTime() ;
            message_data.append(DT_Stamp) ;
            message_data.append(" LU62XnsCvr Method:ProcessRqsts ") ;
            message_data.append("Read Error Code:") ;
            message_data.append(AB_RQST) ; 
            message_data.append("  LU62XCE0513: Request File IO Exception =") ;
            message_data.append(" Request File:") ; 
            message_data.append(Rqst_File) ;
            SendM开发者_如何学编程esg() ; 
            pgm_status  = READ_ERROR ;
        }
        if ( pgm_status == READ_ERROR )
            continue ;    //go back and read next record     

    //****  other stuff goes on ....


}  // end of Process Requests Routine

So I'm wondering does java afford a facile way of "exposing" a set of objects built in one method so they can be "accessed" in another method.

Now I do have a public class e.g.

public class LU62XnsCvr extends Object 
  {   

where I have a lot of objects "declared" and accessable by all the methods in the program. However when I tried to define the FileInputStream and FileOutstream objects I got the IOException compile error; i.e the compiler "sees" these constructors as attempts at I/O operations, but no throws or try-catch has been "defined".

From what I've read I cannot code a "throws" exception on a class declaration.

I can build these objects within a method, but of course this "hides" the objects from other methods.

Please forgive me so being so "longwinded" ... but I'm just tryin' to get thru the "territory"

Thanks and Best Regards

Guy


For a quick fix - Declare the variables RqstChnl and Rqst_Bufr as static class attributes (e.g. "outside main"):

 public class MyClass {

   private static FileChannel RqstChnl = null; 
   private static ByteBuffer Rqst_Bufr = null; 

   // ...

   public static void ConnectFiles throws IOException { 
     FileInputStream RqstInp = new FileInputStream(Rqst_File) ; 
     RqstChnl  = RqstInp.getChannel() ; 
     Rqst_Bufr = ByteBuffer.allocate(RQbuflen) ; 

  // ...

(with a second refactoring you should rename fields and methods, they always start with a lower case letter, that's a convention and makes it much easier for us to understand the code - names starting with upper case letters are always class names)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号