This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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: 37926
    
  22
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
 
Similar Threads
How to import classes in a subpackage from another sub-package?
overriding compareTo
constructor problems
help with a class.
How to give color for JRadioButtons?