What is Instance, Empty Constructor in Java

  • Comp Sci
  • Thread starter tking88
  • Start date
  • Tags
    Empty Java
In summary, the conversation is discussing how the equals method works in Java. The first example, using a Teacher class, returns false because the default equals method compares the pointers, while in the second example using strings, the specific equals method for strings compares the values. The solution is to override the default equals method to compare values instead of pointers.
  • #1
tking88
2
0
What is Instance, Empty Constructor ... in Java

How come will return false I put in 2 same name it should return true ?

Code:
Teacher p1= new Teacher("Nelson"); 
System.out.println(p1); 
Teacher p2= new Teacher("Nelson"); 
System.out.println(p2); 

System.out.println(p1.equals(p2));

it print out
Code:
false


I do testing on below it give me ture how come the above not give me true

Code:
p1 = "Nelson";
p2 = "Nelson";
System.out.println(p1.equals(p2));
 
Last edited:
Physics news on Phys.org
  • #2


Without knowing much about Java (i.e. I could be wrong):

The equals method probably has a default implementation that just compares the two pointers and returns true or false depending on it.

so:

Code:
Teacher p1= new Teacher("Nelson"); 
System.out.println(p1); 
Teacher p2= new Teacher("Nelson"); 
System.out.println(p2);

p1.equals(p2)

actually calls (say)

Code:
bool object.equals(object p2)
{
  return p2==this;
}

Whereas... in your second example you had strings and string will have a specific equals method that compares strings as you expect.

So... write an equals that overrides the default one.
 
  • #3


K I did something like this and all ok now it return the true compare value

Code:
public boolean equals(Object o)
{
System.out.println(o instanceof Moof);
System.out.println(((Moof)o).getMoofValue());
System.out.println(this.moofValue); //XXX
if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
{
return true;
}
else
{
return false;
}
 

Related to What is Instance, Empty Constructor in Java

1. What is an instance in Java?

An instance in Java refers to an object that is created from a class. It is a specific occurrence of a class, and each instance of a class has its own unique set of properties and behaviors.

2. How do you create an instance in Java?

To create an instance in Java, you must first define a class. Then, you can use the "new" keyword to create an instance of that class. For example, if you have a class called "Person", you can create an instance of that class by writing "Person person = new Person();" This will create a new instance of the Person class and store it in the variable "person".

3. What does an empty constructor do in Java?

An empty constructor is a special type of method that has no parameters and does not have any code inside of it. It is used to create an instance of a class without setting any initial values for its properties. If a class does not have any constructors defined, it will automatically have an empty constructor.

4. When should an empty constructor be used?

An empty constructor should be used when you want to create an instance of a class without setting any initial values for its properties. This can be useful if you want to set those values later on in your code, or if the class has optional properties that do not need to be set for every instance.

5. Can an empty constructor have access modifiers in Java?

Yes, an empty constructor can have access modifiers such as public, private, or protected in Java. These modifiers control the visibility of the constructor and determine which classes can access it. If no access modifier is specified, the constructor will have a default access level, which means it can only be accessed by other classes within the same package.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top