This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor - help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor - help needed" Watch "constructor - help needed" New topic
Author

constructor - help needed

Arthy
Greenhorn

Joined: Sep 22, 2000
Posts: 9
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

Joined: May 05, 2000
Posts: 13974
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).


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Arthy
Greenhorn

Joined: Sep 22, 2000
Posts: 9
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

Joined: Sep 25, 2000
Posts: 63
Remember u r dealing with a static variable!
Arthy
Greenhorn

Joined: Sep 22, 2000
Posts: 9
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

Joined: Sep 25, 2000
Posts: 63
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

Joined: Sep 30, 2000
Posts: 18
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

Joined: Sep 29, 2000
Posts: 24
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.


Regular
Arthy
Greenhorn

Joined: Sep 22, 2000
Posts: 9
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

Joined: Aug 26, 2000
Posts: 10065
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!!!

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
jQuery in Action, 2nd edition
 
subject: constructor - help needed