my dog learned polymorphism
The moose likes Beginning Java and the fly likes variables have types, objects have class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variables have types, objects have class" Watch "variables have types, objects have class" New topic

variables have types, objects have class

jinn, boy

Joined: May 08, 2001
Posts: 8
hi all,
in the JLS, it says, "variables have types, objects have class". can someone explain this with examples?

Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Objects have class means that objects are created from a blueprint held in the classfile.
Variables have type means that variables are accountable to the class that they are typed as. They can only deal with methods and fields that the class of their type knows about - but they are not actually that class. This allows polymorphism to happen. If the type knows about a method of the type but is not tied into a particular implementation of the method (as it would be if it were really that class), then a subclass sitting in a variable of the superclass type can sneak in it's own implementation of the method. Also know as late binding.

"JavaRanch, where the deer and the Certified play" - David O'Meara
pam rowell

Joined: Jan 21, 2002
Posts: 7
hi this might help you and it might not variables have types like int, char, boolean, byte and classes have odjects. if you think of class like you would think of a Dog whereas Dog is the class and german shepard and husky would be objects of class Dog. they are all dogs but they look different. you could also think of a Ball with basketballs and baseballs being objects in the ball class. hope this helped i didn't really explain it that great.
I agree. Here's the link:
subject: variables have types, objects have class
It's not a secret anymore!