• Post Reply Bookmark Topic Watch Topic
  • New Topic

exposing objects built in one method to be "accessed" in another method  RSS feed

 
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'd like to know if java provides a facile way of "giving" a method access to objects built in another method. Now both methods are called by the same
"main" routine. and they are all part of the same class.
The objects are channel and bytebuffer resources. Tried using the public static accessors, but that only generated compiler errors. I have the "main" method e.g.



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



 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You can either return the object from one method and then pass the object in the other method.
or
2. You can make the objects as part of the class- either as instance variables or static variables.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought in that case the methods and fields ought not to be static; there might be different values in different contexts, which would imply instance fields.
Standard "how do you build a class" question.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought in that case the methods and fields ought not to be static; there might be different values in different contexts, which would imply instance fields.
Standard "how do you build a class" question.


Agree with Campbell, ideally it should be that way. I glanced at the OP's code and found static everywhere so just suggested declaring as "static".
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but a more likely explanation is that he has a book where everything is written in the main method, so they end up marking all fields static regardless
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!