Results 1 to 4 of 4
  1. #1
    ajaykushwaha is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default How to define a variable which can be accessed across the class?

    I want to declare variables (array) that I can access from any class.

    is there any way?

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    use the public modifier, e.g.

    Java Code:
    public int[] CODES;
    There are some other issues you need to decide:
    • Is it really a variable, or is it something that must remain constant (in which case you add the final modifier).
    • Does this belong to an instance of the class, or is it something that you need to be available without creating instances (in which case you make it static).


    Instead of using a public variable, it may be better, depending on your requirement, to keep it private and use modifier methods.

  3. #3
    wc48 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    hahahaha i got it too work!!!!!!!!!!!!!!!!!!!!! DDD

    thanks guys :)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ajaykushwaha View Post
    I want to declare variables (array) that I can access from any class.

    is there any way?
    I think it's better to study little more about object oriented concepts. That's the best solution you can have.

Similar Threads

  1. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 03:29 AM
  2. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 06:27 PM
  3. Which class and variable to choose?
    By kian_hong2000 in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 07:06 AM
  4. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 03:35 PM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

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