Results 1 to 14 of 14
  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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?
    		
    }
    		
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  3. #3
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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.

  4. #4
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Accessing objects out of their scope

    Quote Originally Posted by christopherx View Post
    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.
    I want to create b first in the if-block as some of the things needed for creation of b can be determined only at run-time using command-line arguments, then use same object in else-if. See my post above.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Accessing objects out of their scope

    Quote Originally Posted by christopherx View Post
    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.
    Not true.

    The if statement could be used to create the object unders different circumstances.
    Java Code:
    Foo f;
    if(cond) {
        f = new Foo();
    } else {
        f = new Foo("hello world");
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Accessing objects out of their scope

    Quote Originally Posted by Dreaming View Post
    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.
    This would only be possible if the program is run more than once. Is this what you are trying to achieve?

  8. #8
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Accessing objects out of their scope

    Quote Originally Posted by Dreaming View Post
    But to do this, I must use the same objects and files created in the first block.
    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. #10
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  12. #12
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Accessing objects out of their scope

    That's what serialized objects look like ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Accessing objects out of their scope

    Thanks, yeah I got it.

Similar Threads

  1. Replies: 0
    Last Post: 03-13-2011, 03:54 AM
  2. Accessing objects in array list
    By kev670 in forum New To Java
    Replies: 6
    Last Post: 03-11-2011, 12:49 AM
  3. Vectors - accessing an unknown amount of objects
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 10:45 PM
  4. Accessing objects between classes
    By bikashg in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 12:05 PM
  5. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •