Results 1 to 7 of 7
  1. #1
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default accessing a one class's non static variable from another class

    i have two classes A and B

    public class A{


    }

    public class B{

    public int x=0;

    A abc = New A();

    }
    now my problem is accessing integer x defined in class B from object abc created from class A?

    i know this can be done by making x static,but i'm looking for another way to do it without making x a static variable.
    can someone help me with this?
    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    pass the int into A via a parameter, perhaps a constructor parameter or a method parameter.

  3. #3
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    but it will pass the int as a copy right? this will not change value of x in B if i change it from a. i want to change value of x from object abc
    Last edited by aruna1; 03-31-2009 at 03:57 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This is a bad casing of creeping requirements. You're much better off giving all of your requirements in your first post as we're pretty poor mind readers here.

    Please give some context here. In a practical sense, just what are you trying to do here and why? Tell us the whole story please.

  5. #5
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    sorry about that.so is there any way to do this?

  6. #6
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Radical approach: make A an inner class.

    Since we don't know exactly what you need,
    I'll take it that by any means you want this
    done.

    If you make A an inner class of B,
    A gains total access to all members
    of B.

    Java Code:
    public class B{
    
      public int x=0;
    
      A abc = New A();
    
      public class A{
    
      }
    
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can give A public mutator methods that allow other objects to change the state of its variables, but again it's hard to know if this solution will help you without some context.

Similar Threads

  1. Replies: 7
    Last Post: 03-16-2009, 07:19 AM
  2. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM
  3. About static variable
    By MarkWilson in forum New To Java
    Replies: 5
    Last Post: 06-27-2008, 01:43 PM
  4. Accessing a static resouces in a web app.
    By sean in forum Threads and Synchronization
    Replies: 3
    Last Post: 08-08-2007, 11:51 PM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •