• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor - help needed

 
Arthy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody help me in this
class MyClass
{
static int maxElements;
MyClass(int maxElements)
{
this.maxElements = maxElements;
}
}
public class Mod
{
public static void main(String[] arg)
{
MyClass a = new MyClass(100);
MyClass b = new MyClass(100);

if(a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("Objects have diff values");
}
}
how come the program prints the else part i.e. "Objects have diff values". Aren't they equal.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects are equal only if they point to the same object. Using object1.equals(object2) is the same as object1 == object2 unless the objects are Strings.
From the API:
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
 
Arthy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx for your immediate reply.
One more thing..
When i inserted System.out.println to print the valur of maxElements in class "MyClass" how come it prints 100,100 for each instantiation. if the static variable belongs to the class, should it not print 100, 200?
 
Vivek Nambiar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember u r dealing with a static variable!
 
Arthy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. that is the reason i am asking why is it printing 100,100. It should print 100,200.
I have one more doubt.
ArithmeticException is a runtime exception. Also I think it is not a checked Exception. But when I try to catch it for a divide by 0 try block, it is getting caught. Can somebody please explain this.
thanks
 
Vivek Nambiar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry to come back again, but why do u think it should be 200, instead of 100? And can u tell me the present value of maxElements?
[This message has been edited by Vivek Nambiar (edited September 30, 2000).]
 
Alagu Seenivasan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyClass inherits the "equals()" method from "Object" class.
The "equals()" method in the "Object" class does a "shallow compare". So if you want to use the equals method to check the equality of your objects, you need to override the "equals()" method in your class. ("String" class does this)
I have modified your code to implement the "equals()" method.
<code>
class MyClass
{
private int maxElements;
MyClass(int maxElements)
{
this.maxElements = maxElements;
}
public boolean equals(Object myClassP) {
if (myClassP instanceof MyClass) {
if (this.maxElements==((MyClass)myClassP).maxElements)
return true;
else
return false;
}
else {
return false;
}
}
}
public class Mod
{
public static void main(String[] arg)
{
MyClass a = new MyClass(100);
MyClass b = new MyClass(100);
if(a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("Objects have diff values");
}
}
</code>
---------------------------------
Now about your other question. It is printing 100, 100 because you dont have code to add the value.
the following code will print 100, 200 as you expect
<code>
class MyClass
{
static int maxElements;
MyClass(int maxElements)
{
this.maxElements += maxElements;
}
}
</code>


[This message has been edited by Alagu Seenivasan (edited September 30, 2000).]
 
UPENDRA KANE
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the Arithmatic Exception... It is an UnChecked Exception. That means, compiler will not raise an error if your method does not catch it.However you can write a code to catch it anyway.
 
Arthy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ths for your explanation. At this range, i don't think i will be able to clear the exam. As days near by, I forget even basic things.
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthy,
I had the same feeling: when I finish chapter n I already forgot previous n-1 chapters. Mock exams help a lot, just go through one of them every day and in a month or two you will memorize most of information.
Good luck!!!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic