Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Scope of Variables

  1. #1
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Scope of Variables

    I am optimizing code that is very well organized and one of my main goals is to maintain this organization even with my additions to personalize the program for my individual use. Currently, I am trying to assign a button command to export data to an excel spreadsheet. However, the way the program is structured my export code uses and references variables that are declared after the buttons of the GUI are defined and their individual code is implemented. I would really like to include the code for my newly added button in the same location as all of the other buttons on the GUI. Is there any way to reference my export code while maintaining this structure?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Scope of Variables

    That's an awful lot of text (c'mon, you know programmers hate to read), when an SSCCE would have been much easier to answer. Can you show us some code that demonstrates what you're talking about? The shorter the better.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Scope of Variables

    The problem is it's not exactly a question where I can just provide an SSCCE. Here's my best attempt at an example:
    Line 14-25:Existing code for the individual buttons on GUI
    Line 26-35:Space for the code behind my newly added export button to the GUI
    Line 70-90: Optimized nested for loops which create the workbook and write the .xls file(These reference specific variables that are not created until these region of the code)

    Is there any way to reference the code in lines 70-90 in the button command in line 26-35 in order to keep the general organization and structure the program initially possessed?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Scope of Variables

    Again, without seeing any code, it's almost impossible to tell you whether something is possible or not. You might be looking to pass a parameter to a method, or define a global variable, or something else- we honestly can't answer this question accurately without seeing the code you're talking about. And people don't really have the time to read through an entire program, so we ask for an SSCCE. It should be possible to write a much much smaller program that does the same general thing. Use a simple String variable instead of anything complicated, maybe.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Scope of Variables

    Quote Originally Posted by PineAppleKing View Post
    I am optimizing code that is very well organized
    Why are you optimizing this code - what is the performance problem that makes it necessary?

    Rules of Optimization:
    Rule 1: Don't do it.
    Rule 2 (for experts only): Don't do it yet.

    M. A. Jackson
    Last edited by dlorde; June 11th, 2011 at 05:44 AM.

  6. The Following User Says Thank You to dlorde For This Useful Post:

    KevinWorkman (June 14th, 2011)

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Scope of Variables

    Sounds like you are trying to refactor rather than optimize...big difference. Break the problem down, and think about the functionality: what pieces of code do what, splitting these into Classes and methods. Once things are reduced down to smaller pieces it makes it easier to call from other parts of code. Without an SSCCE we truly can't provide detailed advice, but it seems to me you have a single method for your program flow which should be put through the process I just described to reduce to methods that define a certain functionality/process.

Similar Threads

  1. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 09:13 AM
  2. What does SCOPE actually mean in java language?
    By wAdeski in forum Java Theory & Questions
    Replies: 3
    Last Post: December 1st, 2010, 10:44 AM
  3. The simpler the program - seemingly the more scope for error :p
    By Bacon n' Logic in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 14th, 2010, 06:10 PM
  4. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  5. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM