Results 1 to 5 of 5
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    230
    Rep Power
    3

    Default How can I make variables accessible by multiple methods?

    I have been making a prototype bookings system for some coursework for a while now and have just moved onto the real thing. The old one worked fine but I wanted to clean up my code a bit.

    In the last project I made lots of variables outside of any methods in order for the methods to use them, however I have read that it is bad to do this for some reason, why?

    if it is bad to do this, what can be done instead?
    e.g. I have 200 variables pointing to objects I have created (the seats), if I declare and initialise them in main() how can I still have other methods understand A[1] without it being declared outside of the methods?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How can I make variables accessible by multiple methods?

    In the last project I made lots of variables outside of any methods in order for the methods to use them, however I have read that it is bad to do this for some reason, why?
    Where did you hear this was bad? What was the context in which it was noted to be 'bad'?

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    230
    Rep Power
    3

    Default Re: How can I make variables accessible by multiple methods?

    I'm not too sure, I think I may have came across some people on here saying it (not necessarily on one of my threads). Is it fine to have pretty much as many as I want then?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How can I make variables accessible by multiple methods?

    Too many static variables is a symptom of OOPS denial, and should be avoided. Instance variables, or fields, aren't a problem.

    That said,
    I have 200 variables pointing to objects I have created (the seats)
    should be a use case for an array or a Collection.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gvnreddy145 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: How can I make variables accessible by multiple methods?

    instance variables is not a problem and based on variable usage we can declare, we can specify the variable prototype.

Similar Threads

  1. How to keep variables through multiple methods
    By SethLasky in forum New To Java
    Replies: 11
    Last Post: 07-26-2012, 03:35 AM
  2. Calling Variables in Multiple Methods
    By PrimalScientist in forum New To Java
    Replies: 10
    Last Post: 02-07-2012, 10:26 AM
  3. using variables in multiple methods
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 09-14-2011, 03:44 AM
  4. Replies: 13
    Last Post: 08-23-2008, 09:09 PM
  5. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 04:14 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
  •