Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Problem with declaring an object

  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Problem with declaring an object

    Hi,

    Java Code:
    void newClient(int fee, String fName){
    		
    		Client fName()=new Client(clients.size(),fee,fName);
    		
    		
    	}
    I've tried to create an object of class "Client" and naming it with the input string "fName".
    Java won't allow it : \
    How can I do it?

    Thanks !

    p.s- If I haven't given enough information, please write me and I'll explain.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Problem with declaring an object

    When seeking help with an error, always post the exact error text, without paraphrasing. "Java won't allow it : \" tells us nothing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Problem with declaring an object

    Error: "Duplicate local variable fName"

    the "fName" is underlined with red.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: Problem with declaring an object

    You have a parameter for thee method called 'fName'.
    You can't also have a local variable called that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Problem with declaring an object

    The thing is I'm not trying to call the object "fName", but I want to use the input name from the user as the name of the new object.
    let's say fName= "smith", then I want the object to be declared Client smith= new Client();

    but for every object created there'll be a different fName, and I want to name every new object by his fName.

    Is it possible?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Problem with declaring an object

    You're confusing a variable identifier with its content. Objects don't have names.

    Also, once you resolve the issue of duplicate variable names, you'll get another error as that line isn't valid Java syntax.

    db
    Mapisto likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Problem with declaring an object

    I see.

    Well, eventually that's what I've done:

    Java Code:
    void newClient(int fee, String fName){
    		
    		clients.add(new Client(clients.size(),fee,fName));
    		
    		
    	}]
    (clients is an ArrayList)

    I'm a bit rusty but I'll remember from now on that objects don't have names :)

    Thanks for the help!

Similar Threads

  1. Declaring variables on the fly
    By sihyde in forum New To Java
    Replies: 2
    Last Post: 04-20-2012, 02:35 PM
  2. Declaring URL object
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 08-03-2009, 07:05 PM
  3. Declaring variables
    By Flashmatix in forum New To Java
    Replies: 12
    Last Post: 01-20-2009, 03:43 PM
  4. Declaring an ArrayList
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 07:36 PM
  5. Declaring Interface
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •