Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Link objects and calling a variable object's method

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Link objects and calling a variable object's method

    Hello,

    I've been searching quite a while for an answer but seeing as I could not find it, I decided to come here, hoping someone could help me.

    Say I have two classes, Author and Book, and I have 2 author objects and 10 book objects. I would like to know how to do two things:

    1) Make some sort of connection that makes clear that author X wrote books A, B and F.

    2) Call a method from a book object that is connected to an author.

    Seeing as I don't know which books will be connected to an author, is there some way to call a method of an object bases on a variable object name? Something like call the getFirstPage() method for every book that is linked to author X?

    Many thanks in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    1) Have an arraylist in the Author's class that contains the books he wrote
    and/or have a field in the Book class that has the address of the Author that wrote it.

    2) Not sure what the problem here is. Methods in the Book class should be able to call methods in the Author class. That would be easy if the Book class has a reference to its Author.

    --- Update ---

    Also posted at: Link objects and calling a variable object's method
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Norm View Post
    1) Have an arraylist in the Author's class that contains the books he wrote
    and/or have a field in the Book class that has the address of the Author that wrote it.

    2) Not sure what the problem here is. Methods in the Book class should be able to call methods in the Author class. That would be easy if the Book class has a reference to its Author.

    --- Update ---

    Also posted at: Link objects and calling a variable object's method
    Thanks, my second question could be asked in a more simple way I guess: How can I call a method from an object based on a variable object name?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    How can I call a method
    referenceToClass.methodName(argsToMethod);
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Sorry to ask this before trying but I'm not currently behind my computer:

    Say I make a new variable, call it targetBook and give it a value of "bookA"

    would targetBook.methodName(argsToMethod) do the same as bookA.methodName(argsToMethod)? because that's what I'm hoping to do.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    No problem. I'll wait until you can try the code you are asking about with a compiler to see what happens.
    Copy any error messages and paste them here with the code.
    Be sure to wrap the code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Norm View Post
    No problem. I'll wait until you can try the code you are asking about with a compiler to see what happens.
    Copy any error messages and paste them here with the code.
    Be sure to wrap the code in code tags.
    Alright, this is my very short example code:

    public class TestThing {
     
    	public void doSomething(){
    		System.out.println("succes");
    	}
    }

    public class Test {
     
    	public static void main(String[] args) {
     
    		String[] arrObjectNames;
    		arrObjectNames = new String[3];	
     
    		arrObjectNames[0] = "objFirst";
    		arrObjectNames[1] = "objSecond";
    		arrObjectNames[2] = "objThird";
     
    		TestThing objFirst = new TestThing();
    		TestThing objSecond = new TestThing();
    		TestThing objThird = new TestThing();
     
    		arrObjectNames[2].doSomething;
     
    	}
     
    }

    I'm trying to call the doSomething() method from the objThird object of the TestThing class by using the value stored in arrObjectNames[2]. This does not work as Java can't find the method this way.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Syntax error, insert "AssignmentOperator Expression" to complete Expression
    	doSomething cannot be resolved or is not a field
     
    	at Test.main(Test.java:17)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    The compiler recognizes a method call by the ()s at the end of the symbol.
    doSomething is missing the ()s
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Norm View Post
    The compiler recognizes a method call by the ()s at the end of the symbol.
    doSomething is missing the ()s
    Whoops, my bad, sorry. It still doesn't work though

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The method doSomething() is undefined for the type String
     
    	at Test.main(Test.java:17)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    The method doSomething() is undefined for the type String
    The compiler can not find the method: doSomething() in the String class.
    The arrObjectNames array holds String objects.

    That method is in the TestThing class. You need to have a reference to an instance of the TestThing class to call the doSomething() method. For example: objFirst is a reference to the TestThing class. Use that.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Wice (June 15th, 2014)

  12. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Norm View Post
    The compiler can not find the method: doSomething() in the String class.

    That method is in the TestThing class. You need to have a reference to an instance of the TestThing class to call the doSomething() method. For example: objFirst is a reference to the TestThing class. Use that.
    		arrObjectNames[0] = "objFirst";
    		arrObjectNames[1] = "objSecond";
    		arrObjectNames[2] = "objThird";
     
    		TestThing objFirst = new TestThing();
    		TestThing objSecond = new TestThing();
    		TestThing objThird = new TestThing();

    I don't udnerstand, isn't that what I already did here?

    EDIT:

    I think I understand now, this works:

    public class Test {
     
    	public static void main(String[] args) {
     
    		TestThing[] arrObjectNames;
    		arrObjectNames = new TestThing[3];	
     
    		TestThing objFirst = new TestThing();
    		TestThing objSecond = new TestThing();
    		TestThing objThird = new TestThing();
     
    		arrObjectNames[0] = objFirst;
    		arrObjectNames[1] = objSecond;
    		arrObjectNames[2] = objThird;
     
    		arrObjectNames[2].doSomething();
     
    	}
     
    }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    Look at the statement the compiler error is on.

    isn't that what I already did here
    What is it that you did there?

    If you want to use a String to get a reference to an existing instance of a class, use a Map. Something like: Map<String, TheClass>
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Norm View Post
    Look at the statement the compiler error is on.


    What is it that you did there?

    If you want to use a String to get a reference to an existing instance of a class, use a Map. Something like: Map<String, TheClass>
    I think I was thinking too much in a PHP way, I changed the array from a string type, to a TestThing type and got it working like this:

    public class Test {
     
    	public static void main(String[] args) {
     
    		TestThing[] arrObjectNames;
    		arrObjectNames = new TestThing[3];	
     
    		TestThing objFirst = new TestThing();
    		TestThing objSecond = new TestThing();
    		TestThing objThird = new TestThing();
     
    		arrObjectNames[0] = objFirst;
    		arrObjectNames[1] = objSecond;
    		arrObjectNames[2] = objThird;
     
    		arrObjectNames[2].doSomething();
     
    	}
     
    }

  15. #14
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    465
    Thanks
    0
    Thanked 89 Times in 74 Posts

    Default Re: Link objects and calling a variable object's method

    By the way, you dont need an array in the first place, you can simply do:
    public class Test {
     
    	public static void main(String[] args) {
    		TestThing objFirst = new TestThing();
    		objFirst.doSomething();
    	}
     
    }

  16. #15
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Cornix View Post
    By the way, you dont need an array in the first place, you can simply do:
    public class Test {
     
    	public static void main(String[] args) {
    		TestThing objFirst = new TestThing();
    		objFirst.doSomething();
    	}
     
    }
    Well, I was working on something that could call a method of an object without calling a specific object by its name.

  17. #16
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    465
    Thanks
    0
    Thanked 89 Times in 74 Posts

    Default Re: Link objects and calling a variable object's method

    I dont think I understand.
    Can you give us any useful scenario where you would like to do this?

  18. #17
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Quote Originally Posted by Cornix View Post
    I dont think I understand.
    Can you give us any useful scenario where you would like to do this?
    Well, I could have a Team object, inside it is an array with Player objects. There could be 3 players but there just as well could be 11 players. At some point I want to call a method from every player object in this team object.

  19. #18
    Junior Member
    Join Date
    Jul 2014
    Location
    Philly area
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    I would design it as a Person, an Author, and a Book.

    This is what I'm thinking:

    public class Person  {
       String getName();
       Date getBirthday();
    }
     
    public class Author  {
       Person getPerson();
       AuthorType getType();
    }
     
    public enum AuthorType {AUTHOR, CO_AUTHOR, ILLUSTRATOR, ...};
     
    public class Book  {
       List<Author> getAuthorList();
       String getTitle();
       String getSubTitle();
    }

    The reason to separate Person from Author (and for the AuthorType enum) is because they might be an AUTHOR in one book, and a CO_AUTHOR in another.

    To go the other way around, you might have a Map where the key is an Author, and the value is a List of all their Books.

  20. #19
    Junior Member
    Join Date
    Jun 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Link objects and calling a variable object's method

    Thanks aliteralmind but I don't understand how that would allow me to do something like:

    arrObjects[2].doSomething();

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Link objects and calling a variable object's method

    arrObjects[2].doSomething();
    If arrObjects is an array of instances of a class that defines the doSomething() method, that should work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Spring 3.2 -- 404 error calling a controller method from a link in a JSP
    By FHSerkland in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2013, 07:00 AM
  2. static variable accessible in object b, not in object a?
    By Pajaro in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 02:41 PM
  3. Replies: 1
    Last Post: February 14th, 2012, 06:42 AM
  4. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 09:17 AM
  5. calling a method from other object: thred
    By Sudheera in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2011, 05:19 AM