Results 1 to 20 of 21
- 09-10-2009, 02:07 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
- 09-10-2009, 02:39 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You can create an instance of a class to call the methods.
Maybe if you post your code better suggestions can be given.
- 09-10-2009, 03:51 PM #3
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:
- 09-10-2009, 03:54 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
Ok, if you I have the following classes
How can I call the method get from the main class??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() { .. } .. }
Thanks.
- 09-10-2009, 04:02 PM #5
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
- 09-10-2009, 04:14 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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,
- 09-10-2009, 04:25 PM #7
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
- 09-10-2009, 04:32 PM #8
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:
- 09-10-2009, 04:36 PM #9
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
- 09-10-2009, 05:06 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
- 09-10-2009, 05:13 PM #11
oops, yes, the original "get" or the renamed "open" needs to be public.
My Hobby Project: LegacyClone
- 09-10-2009, 09:24 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Reading Sun's Java tutorial might not be a bad idea at this stage.
- 09-11-2009, 04:33 PM #13
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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:
Why am I getting an error for this? What is the solution?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() { .. } .. }Last edited by jboy; 09-11-2009 at 04:41 PM.
- 09-11-2009, 04:40 PM #14
- 09-11-2009, 04:41 PM #15
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 ExceptionRamya:cool:
- 09-11-2009, 04:43 PM #16
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
I have editted my code, please take a look at it again..
- 09-11-2009, 04:49 PM #17
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:
- 09-11-2009, 05:20 PM #18
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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:
but I am not allowed to.Java Code:main class: Door.make();
- 09-11-2009, 06:57 PM #19
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The longer you postpone reading Sun's tutorial the more confusion you are going to drag yourself into.Java Code:Door aDoor = new Door(); aDoor.close();
- 09-12-2009, 03:27 AM #20
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
another NullPointerException in LinkedList class
By muffstuff in forum New To JavaReplies: 7Last Post: 04-10-2009, 10:51 PM -
Private or Protected access for super class variables
By Madushan in forum New To JavaReplies: 3Last Post: 03-14-2009, 07:22 AM -
Error: Cannot access protected member long getTimeInMillis() in class Calendar
By cachi in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:53 AM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM -
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks