Results 1 to 14 of 14
- 10-31-2011, 01:16 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Accessing objects out of their scope
Java Code:public class A { class B b; public static void main(String[] args) { if(something) { // work on some command-line arguments relevant to creating object b; // now create b b = new B(); } else if(somethingelse) { //do something else with b //Now the problem is I created b inside the then-block and I can't access it //here in the else-this block but I want to be able to do that //how can I do this? } } }
- 10-31-2011, 01:41 AM #2
Re: Accessing objects out of their scope
If the execution of the program has entered the else if branch then it DID NOT enter the if branch and therefore the B object was not created. You would also need to have code in the else if branch to create the B object.
- 10-31-2011, 01:46 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: Accessing objects out of their scope
In which case, if you're creating b in both the if, and else if options, then you may as well just create "b" beforehand.
- 10-31-2011, 01:55 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Accessing objects out of their scope
OK, you are right. But this is the problem I have. I have to write objects to a file (serialise) and then print them out using command-line arguments. So let us say the first if-statement was executed and it serialised and wrote objects to a file. The second else-statement is executed to deserialise and print out the serialised objects. Say, you have:
command 1: create filename1 filename2
Now in main, I look for the keyword "create" and when I see this, I create ten objects of type computer and ten of type software, save them in filename1 and filename2 respectively.
You then type a second command-line argument: output filename1 filename2. Now, the keyword here is "output", and in main if I get this word, I print out all instances of computer and software that I created before and wrote them to filename1 and filename1.
My thinking maybe silly but I was thinking of first creating the ten objects in the first if-statement when args[0].equals("create"), and then in the print them all out in the else-this block when args[0].equals("output"). But to do this, I must use the same objects and files created in the first block.
- 10-31-2011, 01:59 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
- 10-31-2011, 02:07 AM #6
- 10-31-2011, 02:09 AM #7
- 10-31-2011, 02:12 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Accessing objects out of their scope
Yeah because doing it all at once will not be possible (I guess you must always write first before you can read). So I run first create filename1 filename2 and serialise all computer and software objects in their respective files, then run the program again a second time with output filename1 filename2 command and this time deserialise and read from the files written earlier all instances of computer and software and print them out.
- 10-31-2011, 02:33 AM #9
Re: Accessing objects out of their scope
All the objects you create in the if branch have been serialised and saved to the file. All you have to do in the else branch is to read those objects back into your program. There is no "using" stuff created in the if branch.
Java Code:Foo f; if(create) { f = new Foo(); create output stream write f to file } else if(output) { create input stream f = read object display object } else { Invalid option }
- 10-31-2011, 02:51 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Accessing objects out of their scope
Maybe it is a bit complicated but f is important for me as it has some important methods that I use on the computer and software objects.
- 10-31-2011, 03:01 AM #11
Re: Accessing objects out of their scope
You really are not making much sense. If you have some other class that is used to manipulate your other objects then I assume that you will always need to have that object regardless of what you are doing (reading or writing) in that case always create the object before the if statement.
- 10-31-2011, 05:02 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Accessing objects out of their scope
Thanks yeah, you are right. Sometimes when one is new, I think they tend to "overcomplicate" things. But yeah, I actually could do without all those complication, I could declare it before the if without any problems.
Anyway, now I am done, and all seems well, so far. Except one thing. When I try to open the files where I wrote in the objects, the contents seem weird, a mixture of strange characters, although you could read in them the names of the member variables, is this normal? I though when I open, I might be able to read the contents just as they are printed out.
- 10-31-2011, 09:38 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Accessing objects out of their scope
That's what serialized objects look like ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-31-2011, 09:36 PM #14
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
What is the lifetime (scope & visibility) of containers and their child objects
By NoSwing in forum AWT / SwingReplies: 0Last Post: 03-13-2011, 03:54 AM -
Accessing objects in array list
By kev670 in forum New To JavaReplies: 6Last Post: 03-11-2011, 12:49 AM -
Vectors - accessing an unknown amount of objects
By counterfox in forum New To JavaReplies: 1Last Post: 05-07-2010, 10:45 PM -
Accessing objects between classes
By bikashg in forum New To JavaReplies: 7Last Post: 05-06-2010, 12:05 PM -
Accessing objects from within Action listener
By cog in forum New To JavaReplies: 4Last Post: 12-24-2009, 08:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks