Results 1 to 3 of 3
  1. #1
    karthickd2009 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Question How to use the same variable in different packages ?

    Hi, Can anyone pls. reply how to use the same runtime variable in different packages or different classes in same package...?

    I'm using NetBeans. I created a variable called 'x'. And i assign some value to x after doing some calculations(like addition).
    If i call the variable 'x' in different packages or different java classes in same packages it give zero (0) only. It didn't give that particular runtime answer.
    How to solve this....?

    Thanks for you Help to Clarify me...!!!!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: How to use the same variable in different packages ?

    If you want to share data between applications then you need ot write it to a file or database.

    If you want to share it between classes in the same application then the second class/object needs to have a reference to the first object.

    eg:

    FirstObject obj1 = new FirstObject();
    SecondObject obj2 = new SecondObject(obj1);

    Now the SecondObject has access to the FirstObject by calling methods.

  3. #3
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: How to use the same variable in different packages ?

    I would have the class that contains variable "X" have a getter method, returning variable x. Then, in the class requiring that variable, assign variable x to a variable in the class requiring it.

Similar Threads

  1. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 07:22 PM
  2. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  3. Packages
    By urbim in forum Eclipse
    Replies: 2
    Last Post: 07-02-2009, 03:01 PM
  4. Replies: 3
    Last Post: 02-12-2008, 12:03 PM
  5. Using packages
    By prfalco in forum New To Java
    Replies: 5
    Last Post: 01-31-2008, 11:38 PM

Tags for this Thread

Posting Permissions

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