Results 1 to 9 of 9
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default Reach a variable outside block.

    Hello all.

    Show my problem with an exempel.

    Java Code:
              for (int i=0; i < store.getSize(); i++) {
               var = "hello";
              }
              System.out.println(var)
    Is there a way to reach that "var" outside the block?

    Peace / ocean.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    ur problem is not clearly explain.Do u want to access var outside the for loop?.Then u declare it outside.
    Ramya:cool:

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

    Default

    If you have declared the 'var' variable outside the scope of that for loop it will still exist when the loop has finished; I don't really understand your problem ...

    kind regards,

    Jos

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    New to this kind of code so mabey im all lost here. What im trying to do is get data´s value into my JTable. And this is how far i come.

    Java Code:
     for (int i=0; i < store.getSize(); i++) {
          Item show = (Item)store.getItem(i);
        Object[][]  data = {{show.getId()......... },};
    }
    JTable table = new JTable(data,columnNames);
    This only work if i put JTable inside the loop. And that i dont want. Is there a way to get data outside the for loop?

    /Ocean

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You don't want to create the Object[][] inside the loop either. You only want to populate it inside the loop not creating it over and over again. Also if you declare it outside the loop then you will be able access it after the loop.

  6. #6
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Hm what you say make sence. So i tryed.

    Java Code:
    for (int i=0; i < store.getSize(); i++) {
         Item show = (Item)store.getItem(i);
    }
    Object[][]  data = {{show.getId(), show.getName(),  show.getPrice(), show.getQuantity(), show.getLowQuant()}};
    Have a hard time understand how this is done. This way show.getId() dont work. How can i populate Object inside the loop?

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are going to have to start thinking about how the code works. If you want the Object[][] data to have its values populated by the loop then surely you must declare the Object[][] data before the loop so you can set it's data inside the loop.
    Also think about what Object[][] data can store vs what store.getItem(i) contains.

  8. #8
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Thank you r035198x, After many hours of reading and reading I finnaly think I understand. Have totaly re writen all code and have just one more for loop to figure out then im done. =)

  9. #9
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Just to add some clarification, a variable only exists within the scope that it is declared. This means wherever you first define a variable is where the variable will exist. How do you know what scope the variable is in? Look to see inside which set of curly brackets in which you created the variable. Only code within those brackets (including other curly brackets inside of those) will for the most part have direct access to the variable.

    It gets slightly more complicated if you define a run method when instantiating certain classes, but this is the general jist of it.

Similar Threads

  1. Java failed to reach to a PC in local LAN
    By JavaEmpires in forum Networking
    Replies: 1
    Last Post: 01-24-2008, 07:30 PM
  2. Programming block
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 12:20 PM
  3. Return to try block
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-11-2007, 09:58 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
  •