Newbie Question (Strings)

Discussion in 'Java' started by the clansman, Sep 28, 2003.

  1. the clansman

    the clansman Guest

    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 );
    }
    }

    --------------------
    What's wrong ?
    Thanks
     
    the clansman, Sep 28, 2003
    #1
    1. Advertising

  2. the clansman <> scribbled the following:
    > 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";
    }
    }

    > --------------------
    > What's wrong ?
    > Thanks


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

    --
    /-- Joona Palaste () ---------------------------\
    | 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
     
    Joona I Palaste, Sep 28, 2003
    #2
    1. Advertising

  3. the clansman

    Antares Guest

    > 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 ;-)
     
    Antares, Sep 28, 2003
    #3
  4. the clansman

    the clansman Guest

    That worked perfectly. Thanks!

    "Joona I Palaste" <> wrote in message
    news:bl72ek$s6e$...
    > the clansman <> scribbled the following:
    > > 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";
    > }
    > }
    >
    > > --------------------
    > > What's wrong ?
    > > Thanks

    >
    > I've explained what's wrong. For future reference, please read an
    > introductory Java textbook.
    >
    > --
    > /-- Joona Palaste () ---------------------------\
    > | 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
     
    the clansman, Sep 28, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    716
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    386
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    573
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page