• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor - help needed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember u r dealing with a static variable!
 
Arthy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic