Results 1 to 6 of 6
  1. #1
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default accessing variable/methods of a instantiated object

    Hi,


    I wonder if someone can tell if I instantiate an object issuing :

    Driver n1 = new Driver();

    and within this class I have all my main structures like hashmap's ArrayList's and I'm issuing this from the main class then how if I need to access anything from another class without having to instantiate again what would the best way to do it. Thanks again in advance.

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

    Default

    Quote Originally Posted by hariza View Post
    I wonder if someone can tell if I instantiate an object issuing :
    Java Code:
      Driver n1 = new Driver();
    Yes you can as long as it has either no explicit constructor or a constructor without parameters.

    and within this class I have all my main structures like hashmap's ArrayList's and I'm issuing this from the main class then how if I need to access anything from another class without having to instantiate again what would the best way to do it. Thanks again in advance.
    One way is to give this class getter or accessor methods: Private Data

    Luck!

  3. #3
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Hi Darryl,

    Thanks again. In this class what I have as the contructor is the definition of the HashMaps's like :


    public Driver() {
    Cons = new HashMap<String, Cons>();
    Trips = new ArrayList<Trips>();
    IdMap = new HashMap<String, IdMap>();

    }
    So if I issue the Driver n1 = new Driver(); from the main so I can instantiate and load some initial data to the data structures how can I access it without having to instantiate again?.

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

    Default

    Quote Originally Posted by hariza View Post
    Hi Darryl,
    Darryl??

    I'm much better looking than he is. ;)
    As Ted Turner allegedly said, if I were modest, I'd be perfect!

    Java Code:
    public Driver() {
       Cons = new HashMap<String, Cons>();
       Trips = new ArrayList<Trips>();
       IdMap = new HashMap<String, IdMap>();
    
    }
    So if I issue the Driver n1 = new Driver(); from the main so I can instantiate and load some initial data to the data structures how can I access it without having to instantiate again?.
    Please don't use quote tags for code but instead use code tags.

    I'm confused by how all your class field names seem to match those of a class exactly, for instance, you initiate a variable "Cons" and yet you seem to also have a class named "Cons", same for "Trips" and "IdMap". To avoid confusion, I strongly urge you not to code this way.

    With regard to your question, you need to call the getter methods of the Object of this class that you've created. If it needs to be done in another class or method, then you'll need to pass a reference of the object into the other method or class.

  5. #5
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Sorry about the name confusion. :-)
    I didn't mean it.

    Also sorry by the confusion about my code but actually the Original code is :



    Java Code:
    public Myti() {
    		Users = new HashMap<String, Users>();
    		Journeys = new ArrayList<Journeys>();
    		UserIdMap = new HashMap<String, String>();
    		travelPass = new HashMap<String, travelPass>();
    		Myti = new HashMap<String, Myti>();
    
    		Stations = new HashMap<String, Stations>();
    		Zones = new HashMap<String, Zones>();
    }
    and then in the main class I'm issuing Mity n1 = new Myti();
    Last edited by hariza; 10-11-2010 at 02:27 AM. Reason: tags not properly set.

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

    Default

    Quote Originally Posted by hariza View Post
    Sorry about the name confusion. :-)
    I didn't mean it.
    No need to worry, because for the record Darryl in truth is better looking and smarter than I.

    Also sorry by the confusion about my code but actually the Original code is :
    ...
    Why are you still using quote tags for your code, since if you do this, you lose all your code formatting. Again, please use code tags. You can edit your post above and change this without having to add another post.

    You also need to tell us specifically what problem you're having if any.

Similar Threads

  1. Accessing GUI object methods
    By km0r3 in forum AWT / Swing
    Replies: 5
    Last Post: 10-06-2010, 02:39 AM
  2. Error Accessing Methods in My JAR file
    By avu in forum Advanced Java
    Replies: 4
    Last Post: 03-23-2010, 01:35 AM
  3. same object variable t and same methods is calling
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 05:10 AM
  4. Replies: 6
    Last Post: 03-31-2009, 05:27 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 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
  •