File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Passing Objects into Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Objects into Methods" Watch "Passing Objects into Methods" New topic

Passing Objects into Methods

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have the following code:

I get the following error:
C:\java>javac cannot resolve symbol
symbol : variable vehicleTypes
location: class java.lang.String
convertedObject = toString( ).vehicleTypes[i];

Could someone explain why it can not resolve the symbol vehicleTypes.

(Thanking you in advance).
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

What the compiler is telling you is it is looking for a variable called vehicleTypes in the String class. Why is it looking for this variable in the String class? Its the line:

What you are asking here is for an object of whatever class this method is in to call toString() (which returns a String), then access the variable vehicleTypes. Of course when toString() is finished, you are dealling with a String, not whatever object has a public static array called vehicleTypes.
[ November 11, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529


From what you have posted so far, I can only glean the following:
1. You have a Vehicle class
2. You have been trying to System.out.println(Vehicle) but don't
want the result to be the default (the result of toString()).

So, here's what I think you are trying to do:

Alternatively, you could override Vehicle.toString():


Junilu - [How to Ask Questions] [How to Answer Questions]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Junilu Lacar,

Many thanks with your response. Much appreciated.

Following what you said above I decided to create a class that I can use over and over again. I called it Override. This class is used when I try to System.out.println (.............. what ever), when I don't want the result to be the default (the result of toString ( ) ). As you mentioned.

So my class is like this:

This class compiles and executes as expected but when I try to use it, it is a different matter.

public void storeStudent (Student studentObject [ ])
for (lp = 0; lp<numberOfStudents; lp++)
System.out.println ("\nstudentObject Before override: "+studentObject[lp] );
//Over ride toString
System.out.println ("\nstudentObject After override: "+studentObject[lp] );

The offending code is when I call the Override class and pass the studentObject[lp] into it.

My first thoughts were in my Override class I declared a variable of 'type' String when it should perhaps be Student - however this was unsuccessfull.

Could you point me in the right direction to correct this? Or alternatively suggest what I am doing wrong?

Thanking you in advance!
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

What you've done is a little confused Maureen. Have another look at the second Vehicle class Junilu Lacar posted. What that does is provide a toString method specific to the Vehicle class, which overriders the one it inherits from the Object class (which all Java Objects implicitly extend). You don't need a seperate class to override the toString() method, just override the method in your Vehicle class.

Where you do this:

I'm suprised you say it compiles - it shouldn't unless you have a method called "Override" in the same class you have this line. I think you have may confused how you call a method on another object. There's two ways:
  • By creating an instance of a new object and calling a method on that instance e.g.

  • Or by calling a static method, like this:

  • As I've said though, if all "Override" is there to do is override the toString method of another object, then its unneccessary. You just need to add the toString() method to your Vehicle class.

    Any questions - don't hesitate to ask.
    [ November 15, 2004: Message edited by: Paul Sturrock ]
    I agree. Here's the link:
    subject: Passing Objects into Methods
    It's not a secret anymore!