10 Replies - 655 Views - Last Post: 15 January 2013 - 04:35 PM Rate Topic: -----

#1 W1174503  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-January 13

Constructor is undefined

Posted 15 January 2013 - 02:23 PM

Eclipse allows me to fix the undefined constructor (line 4 of TestToString.java) by removing the arguments but that is not what I am trying to do. I am attempting to get the program to execute and say (“Id is 1 The name is Fluffy). Can anyone please help?

public class TestToString {

	public static void main(String[] args) {
		ToString obj1 = new ToString(1, "Fluffy");
		System.out.println(obj1.toString(obj1));

	}

}


public class ToString {
	private int iD;
	private String name;
	
	public void toString(int iDArg, String nameArg){
		iD = iDArg;
		name = nameArg;
	}

	public String toString(ToString Obj1) {
		String ToString = "Id is " + iD + "The name is " + name; 
		return ToString;
	}
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Constructor is undefined

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1057
  • View blog
  • Posts: 2,260
  • Joined: 30-January 11

Re: Constructor is undefined

Posted 15 January 2013 - 02:26 PM

Java is case sensitive - toString != ToString
Was This Post Helpful? 3
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1341
  • View blog
  • Posts: 3,513
  • Joined: 03-May 10

Re: Constructor is undefined

Posted 15 January 2013 - 02:26 PM

The error you're getting is because your constructor is named "toString" while your class name is "ToString". There is a difference in those two spellings, and therefore it is not recognized as a constructor for the class.

EDIT: Quick fingers there, Ryano121. Well done.

This post has been edited by Kilorn: 15 January 2013 - 02:27 PM

Was This Post Helpful? 3
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 196
  • View blog
  • Posts: 1,628
  • Joined: 13-March 10

Re: Constructor is undefined

Posted 15 January 2013 - 02:26 PM

It is a terrible idea to name your class ToString. First of all, there is a toString() method inside the Object class that everyone extends. Then, if ToString was a good idea, it sounds like a method, whereas classes tend to be nouns.
Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5606
  • View blog
  • Posts: 9,049
  • Joined: 19-March 11

Re: Constructor is undefined

Posted 15 January 2013 - 02:29 PM

Your toString method shouldn't take an argument.

And for Pete's sake can't you think of a name that isn't "toString"?

This post has been edited by jon.kiparsky: 15 January 2013 - 02:31 PM

Was This Post Helpful? 2
  • +
  • -

#6 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Constructor is undefined

Posted 15 January 2013 - 02:30 PM

The constructor should have the same name as the class, and the class name is case-sensitive. Also, constructors don't specify a return type. I think you might want something like this:
public class ToString {
	private int iD;
	private String name;
	
	public ToString(int iDArg, String nameArg){
		iD = iDArg;
		name = nameArg;
	}

        // ... not really sure about the second method. 
        // obj1 is never used. Is there a reason for it?
}

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Constructor is undefined

Posted 15 January 2013 - 02:36 PM

and this is wrong

return ToString; // constructor don't return anything

Nobody catch it

*Edit: but NathanMullenax

This post has been edited by pbl: 15 January 2013 - 02:37 PM
Reason for edit:: Yes somebody catched it

Was This Post Helpful? 0
  • +
  • -

#8 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 196
  • View blog
  • Posts: 1,628
  • Joined: 13-March 10

Re: Constructor is undefined

Posted 15 January 2013 - 02:41 PM

*caught it
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5606
  • View blog
  • Posts: 9,049
  • Joined: 19-March 11

Re: Constructor is undefined

Posted 15 January 2013 - 02:44 PM

View Postpbl, on 15 January 2013 - 04:36 PM, said:

and this is wrong
return ToString; // constructor don't return anything
Nobody catch it


To be honest, there's so much wrong in those few lines, it would have been difficult to get it all in.
Was This Post Helpful? 2
  • +
  • -

#10 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 572
  • View blog
  • Posts: 1,752
  • Joined: 29-July 11

Re: Constructor is undefined

Posted 15 January 2013 - 04:03 PM

View Postjon.kiparsky, on 15 January 2013 - 09:29 PM, said:

And for Pete's sake can't you think of a name that isn't "toString"?


I vote for TooString :lol:
Was This Post Helpful? 0
  • +
  • -

#11 W1174503  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-January 13

Re: Constructor is undefined

Posted 15 January 2013 - 04:35 PM

I understand what I have done wrong and fixed it. Thanks everyone for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1