Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Access LinkedList from another class

    Hi,

    Is there any way that I call a linkedlist method from another class?

    For examples, say I have a linkedlist called list in my second class and this class has a method called get(), how can I call the method of get() in this class from the root class?

    Thank you.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You can create an instance of a class to call the methods.
    Maybe if you post your code better suggestions can be given.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    U can also access with other classname if the other class is having static keyword for a method.

    But,we need to check the class is private or need to check the method also for the scope.
    Ramya:cool:

  4. #4
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Ok, if you I have the following classes

    Java Code:
    This is the main class:
    
    public class Factory
    {  .. 
        private LinkedList<Car> cars = new LinkedList<Car>
        ..
    }
    
    My second class:
    
    public class Car
    {  ..
        ..
        ..
        private LinkedList<Door> doors = new LinkedList<Door>
        ..
    }
    
    The third class:
    
    public class Door
    {
      ..
      ..
      private int get()
      { .. }
       ..
    }
    How can I call the method get from the main class??

    Thanks.

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    You could give access to the Car objects Door list by exposing in via a public method:

    Java Code:
    My second class:
    
    public class Car
    {  ..
        ..
        ..
        private LinkedList<Door> doors = new LinkedList<Door>
        ..
       public LinkedList<Door> getDoors()
       {
          return doors;
       }
    }
    My Hobby Project: LegacyClone

  6. #6
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I am not really sure why you are doing it that way, I need to call the get() from the Main class. Could you clarify it for me?

    Thanks,

  7. #7
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Because your main has zero or more cars each with zero or more doors, which "get" exactly do you want to get, there could be a lot of them to choose from. :)
    My Hobby Project: LegacyClone

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    You are having private know.U need to have some method to return or else u can't access.Anyway u are having Factory class know? return the Cars as well as Doors LinkedList via some public method and then try to access.

    As mrmatt1111 said,u cant access the LinkedList as it is because it will have collection of doors as well as cars.U can access with index.

    get() method also private.Anyway ,it is not accessible outside.
    Last edited by RamyaSivakanth; 09-10-2009 at 04:40 PM.
    Ramya:cool:

  9. #9
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    A simple Example:

    Lets say you know you have 10 cars each with 4 doors, and you would like to get the 5th car 1st door's "get"

    Java Code:
    public class Factory
    {
       //some method somewhere
       ...
       System.out.println("The int: " + cars.get(4).getDoors().get(0).get()); //lots of gets, hehe
       ...
    }
    My Hobby Project: LegacyClone

  10. #10
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    It seems there is some misunderstanding if I change the name of get to something so it is a user-defined method, say open() method and change the private to public. Will I still be able to access that method from the main class? because I really need to access the class 3 method from class one.
    Thanks heaps.

  11. #11
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    oops, yes, the original "get" or the renamed "open" needs to be public.
    My Hobby Project: LegacyClone

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Reading Sun's Java tutorial might not be a bad idea at this stage.

  13. #13
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I can't make any progress in my codes because I still have trouble accessing methods from other classes and whatever I do, I get NullPointerException error. Consider the following example:
    Java Code:
    public class Factory
    {  .. 
        private LinkedList<Car> cars = new LinkedList<Car>
        Door door;
        door.make();
        ..
    }
    
    My second class:
    
    public class Car
    {  ..
        ..
        ..
        private LinkedList<Door> doors = new LinkedList<Door>
        ..
    }
    
    The third class:
    
    public class Door
    {
      ..
      ..
      private void make()
      { .. }
       ..
    }
    Why am I getting an error for this? What is the solution?
    Last edited by jboy; 09-11-2009 at 04:41 PM.

  14. #14
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    Car car = new Car();
    My Hobby Project: LegacyClone

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    Hi,
    Please don't put ... Send a complete code ...Please........not at all understandable

    As per ur above code how can u call meth with Car reference without object? Definitley it will thro Exception
    Ramya:cool:

  16. #16
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I have editted my code, please take a look at it again..

  17. #17
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    Where u edited ? again u are trying to call with door reference.Please read Sun tutorial
    send the properly compiling code.................
    Last edited by RamyaSivakanth; 09-11-2009 at 04:57 PM.
    Ramya:cool:

  18. #18
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Sorry guys, if my code doesn't make really sense to you. Actually, I have a couple of classes and it is going to be very long here if I post them. If you could just point out how else I can call the method (make) from the third class, I would be very thankful. Although I am able to use static method and then call the make method from the main class however I have been told not to use static at all. With static I would just go like this:
    Java Code:
    main class:
    Door.make();
    but I am not allowed to.

  19. #19
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Java Code:
    Door aDoor = new Door();
    aDoor.close();
    The longer you postpone reading Sun's tutorial the more confusion you are going to drag yourself into.

  20. #20
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Could you please guide me to where I could find information regarding these sort of things? Where in the Sun tutorial?

Page 1 of 2 12 LastLast

Similar Threads

  1. another NullPointerException in LinkedList class
    By muffstuff in forum New To Java
    Replies: 7
    Last Post: 04-10-2009, 10:51 PM
  2. Replies: 3
    Last Post: 03-14-2009, 07:22 AM
  3. Replies: 1
    Last Post: 08-07-2007, 07:53 AM
  4. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:52 AM
  5. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:08 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
  •