• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Objects into Methods

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code:


I get the following error:
C:\java>javac Vehicle.java
Storage.java:69: 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).
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maureen,

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():



HTH
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Override(studentObject[lp]);
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
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic