3

In below code i'm doing somthing wrong. Sorry if this is a bit basic. I get this working fine if it's all in the one class but not when i break the classes up like in the code below:

class Apples{
    public static void main(String[] args){

        String bucket = "green"; //instance variable

        Apples appleOne = new Apples(); //create new object (appleOne) from Apples class

        System.out.println("Paint apple one: " + appleOne.paint(bucket));
        System.out.print("bucket still filled with:" + bucket);

        }//end main

    }//end class

class ApplesTestDrive{

    public String paint(String bucket){

        bucket = "blue"; //local variable
        return bucket;

        }//end method

    }//end class

Error Message:

location:class Apples
cannot find symbol
pointing to >> appleOne.paint(bucket)

Any hints?

1
  • @EricBoersma, it's there at the bottom, but I edited the question now to make it stand out. Commented Oct 1, 2010 at 15:11

3 Answers 3

5

You need to create an instance of ApplesTestDrive, not Apples. The method is in there.

So, instead of

Apples appleOne = new Apples();

do

ApplesTestDrive appleOne = new ApplesTestDrive();

This has nothing to do with passing by reference (so I removed the tag from your question). It's just a programmer error (as practically all compilation errors are).

Sign up to request clarification or add additional context in comments.

1 Comment

@all thank you very much for putting me on the right track. your help is very valuable to me! Thanks again.
1

You are calling the method paint on Apple object BUT the paint method is in AppleTestDrive class.

Use this code instead:

AppleTestDrive apple = new AppleTestDrive();
apple.paint(bucket);

Comments

0

System.out.println("Paint apple one: " + appleOne.paint(bucket)); paint is a method of ApplesTestDrive class and appleOne is an Apples objject, so you can't call appleOne.paint here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.