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

Thread: How to call a static method within a static method in the same class?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to call a static method within a static method in the same class?

    Hi guys,
    I'm trying to call a static method within a static method of the same class, but I can't figure out how. In the tuneAll method below, I'm trying to call tune(Instrument an Instrument). When I try to compile it by just declaring tune(), it says I need an Instrument argument, so then I put Instrument.tune() and it says tune() cannot be found. What am I doing wrong?

        /**
         * Calls the what(), adjust() and play() methods on the parameter which is an Instrument.
         * 
         * @param anInstrument an Instrument in the hierarchy
         */
        public static void tune(Instrument anInstrument) {
            anInstrument.what();
            anInstrument.adjust();
            anInstrument.play();
        }
     
        /**
         * Tunes each instrument in the ArrayList by calling the tune method.
         * 
         * @param e an ArrayList of Instruments
         */
        public static void tuneAll(java.util.ArrayList<Instrument> e) {
            for (int i=0; i<e.size(); i++) {
                Instrument.tune();
            }
        }


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

    Default Re: How to call a static method within a static method in the same class?

    The compiler can not find a method: tune() that take no arguments?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a static method within a static method in the same class?

    Well tune() takes an Instrument as an argument. So I actually should pass an Instrument argument within the parenthesis, right?. Which means I need to change the Instrument.tune() line to _______.tune(______); but I'm confused as to what.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to call a static method within a static method in the same class?

    It looks to me like you have designed this poorly, it seems to me that tune() should probably NOT be a static method of Instrument, but just a standard method. But in regards to the way you are doing it, you should call it as you were before but pass in the instrument you wish to tune. That is coming from the ArrayList you are looping through.

    Chris

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to call a static method within a static method in the same class?

    ClassName.MethodName()

Similar Threads

  1. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  2. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  3. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM