Results 1 to 9 of 9
Thread: Reach a variable outside block.
- 10-23-2009, 11:21 AM #1
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
- 10-23-2009, 11:24 AM #2
ur problem is not clearly explain.Do u want to access var outside the for loop?.Then u declare it outside.
Ramya:cool:
- 10-23-2009, 11:24 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-23-2009, 11:45 AM #4
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
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.
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?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);
/Ocean
- 10-23-2009, 11:57 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-23-2009, 12:08 PM #6
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Hm what you say make sence. So i tryed.
Have a hard time understand how this is done. This way show.getId() dont work. How can i populate Object inside the loop?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()}};
- 10-23-2009, 12:14 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-30-2009, 11:56 AM #8
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
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. =)
- 10-30-2009, 01:08 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
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
-
Java failed to reach to a PC in local LAN
By JavaEmpires in forum NetworkingReplies: 1Last Post: 01-24-2008, 06:30 PM -
Programming block
By Java Tip in forum Java TipReplies: 0Last Post: 12-25-2007, 11:20 AM -
Return to try block
By Freddie in forum New To JavaReplies: 2Last Post: 05-11-2007, 08:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks