Two Laptop Bag
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

Trying to create a liquid class

Joshua Flowers

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: 15092

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

Joined: Oct 27, 2005
Posts: 20275

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().

How To Ask Questions How To Answer Questions
David Newton

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

Joined: Oct 13, 2005
Posts: 46368
Careful, or I shall have to move this thread to MD
I agree. Here's the link:
subject: Trying to create a liquid class
It's not a secret anymore!