Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: variable not recognised

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default variable not recognised

    public class Person
    {
    	private String name;
     
    	public Person()
    	{
    		name = "No name yet.";
    	}
    	public Person(String n)
    	{
    		name = n;
    	}
    	public void setName(String newName)
    	{
    		name = newName;
    	}
    	public String getName()
    	{
    		return name;
    	}
    	public void print()
    	{
    		System.out.println("Name: " + name);
    	}
    	public boolean equals(Object p)
    	{
    		return name.equals(p.name);
    	}
    }
    the only part that gives me trouble is p.name at the end it says it's not recognising varianle name but it was initialised at the top


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: variable not recognised

    Are you trying to override the equals function for Object? Object has no variable name, but your class does. If so, you must cast Object (highly recommended you check for null and that the parameter is an instanceof your class).

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: variable not recognised

    Quote Originally Posted by gochi7 View Post
    public class Person
    {
    	private String name;
     
    	public Person()
    	{
    		name = "No name yet.";
    	}
    	public Person(String n)
    	{
    		name = n;
    	}
    	public void setName(String newName)
    	{
    		name = newName;
    	}
    	public String getName()
    	{
    		return name;
    	}
    	public void print()
    	{
    		System.out.println("Name: " + name);
    	}
    	public boolean equals(Object p)
    	{
    		return name.equals(p.name);
    	}
    }
    the only part that gives me trouble is p.name at the end it says it's not recognising varianle name but it was initialised at the top
    Are you trying to override the equals function for Object? Object has no variable name, but your class does. If so, you must cast Object (highly recommended you check for null and that the parameter is an instanceof your class).
    It appears the OP is using the equals method of the String class.

    This should work.

     
    public boolean equals(String name2)
    {
    if (getName().equals(name2))
    return true;
    return false;
    }

    Another possibility is:

     
    public boolean equals(Person p)
    {
    if (getName().equals(p.getName()))
    return true;
    return false;
    }

    And to avoid the null thing, have your Person(String name) constructor call the method setName(name);
    public Person(String name)
    {
    this.name = name;
    setName(name);
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: variable not recognised

    It appears the OP is using the equals method of the String class.
    Which is defined in Object, not String
    This should work.
    What makes you think that will work? equals is defined in Object, taking an Object as a parameter...define it any other way and and the behavior may not be what you expect (for example finding an object in a Collection will still use the method defined in Object). To check for equality beyond reference equality, you must override the Object.equals function as it is defined

  5. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 20th, 2011)

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: variable not recognised

    Wait, you mean you have to do it this way:

    public boolean equals(Object obj)
    {
    if (obj instanceof Person)
    {
    if (getName().equals((Person) obj.getName()))
    return true;
    }
    return false;
    }

    Wait... that's how your override a method, true. I'd forgotten.
    But isn't what i did earlier called overloading the method, which is also valid?

    for instance
    public String toString(String aString)
    {
    return aString;
    }

Similar Threads

  1. How to use the same variable in various JPanel?
    By danielpereira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2010, 12:08 PM
  2. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM
  3. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM
  4. Reset the value of each variable
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 22nd, 2009, 09:45 AM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM

Tags for this Thread