Newbie Question (Strings)

T

the clansman

I have an Applet and somewhre in my code, I'd like to have a String variable
value changed by another method. The code looks like below. The compiler
returns the following error:

-------------
"BasicApplet.java:107: incompatible types"
"found :test
required: java.lang.String
cString =new test();
1 error
-------------

import javax.swing.*;
....
....
import java.lang.String;
....
....

class myFrame extends JFrame
{
...
private String cString ;

myFrame( )
{
cString =new test();
}
}


class test
{
String cValue;
String test()
{
cValue = new String( "hello" );
return( cValue );
}
}
 
J

Joona I Palaste

the clansman said:
I have an Applet and somewhre in my code, I'd like to have a String variable
value changed by another method. The code looks like below. The compiler
returns the following error:
-------------
"BasicApplet.java:107: incompatible types"
"found :test
required: java.lang.String
cString =new test();
1 error
-------------

Surprise, surprise. The type of "new test()" is a test, and the
type of cString is a String. You're trying to assign incompatible
types.
import javax.swing.*;
...
...
import java.lang.String;
...
...
class myFrame extends JFrame
{
...
private String cString ;
myFrame( )
{
cString =new test();
}
}

Do you want cString to have the value "hello" returned by the method
test() in class test? If so, then calling the constructor is not
enough. Call the method test() in the test object:

cString = new test().test();

You seem to be thinking that giving a method the same name as a class
automatically makes it a constructor. Not so. If the method has a
return type, then it is a regular method, no matter what name it has.
class test
{
String cValue;
String test()
{
cValue = new String( "hello" );
return( cValue );
}
}

Also, your entire test class could be shortened to:

class test
{
String test()
{
return "hello";
}
}

I've explained what's wrong. For future reference, please read an
introductory Java textbook.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
 
A

Antares

class test
{
String cValue;
String test()
{
cValue = new String( "hello" );
return( cValue );
}
}

cString =new test();
What's wrong ?

test is the name of your class, but also the name of a method in that class.
(It's to avoid this kind of confusion that we have the convention that class
names should begin with a capital letter while method and variable names
should begin in lowercase.)
Your code cString=new test() fails because cString refers to a String while
new test() returns a reference to a test object. What you need is
cString = new test().test()
ie "create a new test object and call its test() method".

Can't tell from the snippets of code, but at first glance it looks like in
neither class does your cValue variable need to be an instance variable.
Instead, declare it in the method in which you use it. Indeed your code
cValue = new String( "hello" );
return( cValue );
could be shortened to
return "hello";

I could be wrong of course: if you need to use the variable in more than
one method, or if its value needs to be remembered over a period of time,
then an instance variable would be appropriate. But you could still get rid
of
new String("hello")
and just use
"hello"
The two are identical. As far as I know, Strings are the only objects in
Java which have a literal form (that is, which can be constructed without
using a constructor). Though I expect someone will correct me on this ;-)
 
T

the clansman

That worked perfectly. Thanks!

Joona I Palaste said:
Surprise, surprise. The type of "new test()" is a test, and the
type of cString is a String. You're trying to assign incompatible
types.




Do you want cString to have the value "hello" returned by the method
test() in class test? If so, then calling the constructor is not
enough. Call the method test() in the test object:

cString = new test().test();

You seem to be thinking that giving a method the same name as a class
automatically makes it a constructor. Not so. If the method has a
return type, then it is a regular method, no matter what name it has.


Also, your entire test class could be shortened to:

class test
{
String test()
{
return "hello";
}
}

I've explained what's wrong. For future reference, please read an
introductory Java textbook.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top