aspose file tools*
The moose likes Beginning Java and the fly likes Trying to create a liquid class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to create a liquid class" Watch "Trying to create a liquid class" New topic
Author

Trying to create a liquid class

Joshua Flowers
Greenhorn

Joined: Mar 09, 2010
Posts: 1
Hi, I am new. So I am trying to create a liquid class that holds an int Density, and a Color color.

unfortunetly, everytime I try to run it I get this error



I really have no idea what I am doing wrong...

here is my code



hope to get some help
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

The variable density is an int - a primitive type. You're trying to call methods (equals() and compareTo()) on an int - but you can't call methods on primitive types.

Use ==, < or > to compare the values of int variables.

And welcome to JavaRanch!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Your class violates the contract of equals and hashCode in two ways:
1) equals should return false if the object is of an incompatible class, not throw a ClassCastException as your class will. Add a check:
2) You need to implement hashCode as well. The easiest solution:
For more information, read Object.equals(Object) and Object.hashCode().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Rob Prime wrote:return densitity

That's what the Italian lady did when her implant turned solid.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Careful, or I shall have to move this thread to MD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to create a liquid class