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 4 of 4

Thread: Having Trouble Linking a method from another class!

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

    Exclamation Having Trouble Linking a method from another class!

    Please read all of this before commenting! Thank you!

    Im fairly new to Java, i use the BlueJ software to develop my programs.

    I am creating a program which holds two data structures; Array List and Binary Tree. I have created a person class which is used to declare variables containing people, this class also contains a method which prompts the user to input 3 characters of a memorable word. These people are created within the array list and binary tree. This actually works in practice, within the program, but i get an error, and i need the program to contain no errors.

    In my binary tree class, i want to call this method.

    I have attached 4 images to this post, an overview of the classes used, a look at highlighted code which is an attempt at calling a method from the person class, and the method contained within the person class itself. Note that the person class is just called "Person". The 4th image is the error being displayed.

    And moderators, if this post is in the wrong place can you please move it as opposed to deleting it, i spent time typing this up and i am going to bed.

    overview.jpgnonworkingcode.PNGpublicvoidmethod.PNGerror.jpg


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having Trouble Linking a method from another class!

    To call a method in a class, you need a reference to that class(ignoring static methods).

    Sometimes you need to pass that reference from one class where it was assigned a value to the class that needs it to make the call.

    Post any code here that shows the problem, Be sure to wrap the code in code tags.
    Also post the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Having Trouble Linking a method from another class!

    (New to Java and unsure of your error, but going to take a shot at it)

    Norm spoke of static methods above. After learning about static methods previously, I recommend making the inputMemorableWord method a static method, if you do not create an instance of the class that holds the inputMemorableWord method.

    Instead of this:
    public void inputMemorableWord() {
       //your code here
    }

    Suggesting this:
    public static void inputMemorableWord() {
       //your code here
    }

    To the more experienced Java programmers, please let me know if I am wrong to correct errors before it becomes a bad habit.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Having Trouble Linking a method from another class!

    Quote Originally Posted by Votek View Post
    ...I recommend making the inputMemorableWord method a static method...

    To the more experienced Java programmers, please let me know if I am wrong to correct errors before it becomes a bad habit.
    This fully depends upon how one wishes to use the method (unclear - at least to me - at this point) Declaring the method as static precludes one from using any sort of object oriented programming, thus the method cannot access instance fields of the object if it needs to.

Similar Threads

  1. gui not linking with main project/ class
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 15th, 2013, 07:44 AM
  2. having trouble with JTextField selectAll method
    By Daddyspike in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2013, 05:40 PM
  3. Noobie Object/Class Linking help
    By Bonerize in forum Object Oriented Programming
    Replies: 4
    Last Post: January 31st, 2013, 08:40 AM
  4. Linking class's
    By Tate in forum Object Oriented Programming
    Replies: 3
    Last Post: February 23rd, 2012, 04:12 AM
  5. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM

Tags for this Thread