• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with initialization  RSS feed

 
Shio Nakashima
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone

I really am new to Java and have hit my first hurdle.

I have declared 3 instance variables:
and created 2 objects t222 and z333. (no problems here)

I am now wanting to set the studentCode of the course object referenced by t222 to "T222" and the same for the studentCode, from z333 to "Z333".

I know what I have is not right because I keep getting the error:
incompatible types; found: student.Course, required: java.lang.String at line 29, column 12 I have



Could anyone point out to me PLEASE what I am doing wrong becuase I am going round in circles.

Any help would be much appreciated.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable T222 is declared as type String. You're trying to assign an instance of Course to it.

A Course is not a String.

(PS: Have you seen the JavaRanch Naming Policy? You can edit your name here. Thanks!)
[ February 11, 2006: Message edited by: marc weber ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since T222, etc. are declared to be of type String, you cannot assign the reference to point to an object other than a String. (For other classes, you could point a reference to an object of that class or any of its subclasses, but String is a final class and you can't create a subclass of it.)
 
Shio Nakashima
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for answering. I have now changed the name.

Although I understand what you are saying, is it possible you could post an example so I can see where I have gone wrong.

Thanks
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to guess what you were trying to do, so forgive me if I go astray.



So, the following would work:

 
Shio Nakashima
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Craig, I see it now....feeling pretty foolish but glad that I found this message board.

Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!