wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Methods and Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Methods and Classes" Watch "Methods and Classes" New topic
Author

Methods and Classes

Kevin Althaus
Greenhorn

Joined: Nov 13, 2012
Posts: 3
I ask a lot of questions just to make sure I understand what's going on.

A class contains methods (or functions), correct?

When does a class become an object?

Thanks in advance.


....I'm workin' on it!
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

A class does not become an object. You can think of a class as being a "cookie-cutter" and an object as being a "cookie".
Kevin Althaus
Greenhorn

Joined: Nov 13, 2012
Posts: 3
So the class creates the object. The class itself isn't the object.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It's more like the class defines what a particular kind of object is--what state it stores (member variables) and what operations it can perform (method). It's like a blueprint, or a template, or a definition.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Yes a class is a blueprint and objects can be made using it.

for example you can have a Dog class



see so here we have a Dog class (but note no objects as yet) that defines that a Dog has a name, a size and a type. it also has a method that describes a dog barking.

so now we can create multiple objects from this class:



I hope that makes sense?
Kevin Althaus
Greenhorn

Joined: Nov 13, 2012
Posts: 3
Yes, it's starting to make more sense.

I'm working thru some tutorials on this and it's all coming together.

dog1, dog2, dog3 are the objects created from the class Dog. The methods are used to construct the object. Would it be safe to say that the class is just a container for the methods?

Thank you for taking the time answer this.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Kevin Althaus wrote:Yes, it's starting to make more sense.

I'm working thru some tutorials on this and it's all coming together.

dog1, dog2, dog3 are the objects created from the class Dog. The methods are used to construct the object. Would it be safe to say that the class is just a container for the methods?

Thank you for taking the time answer this.


yes they are all objects of class Dog

the "method" used to construct them isn't actually a method, it is called a constructor, it looks very similar to a method yes but notice there is no return type.

but in short yes the "method" for creating objects of class is called a constructor, and sometimes if you do not see one in code or you dont want to pass values in and let it take the defaults then there is an implicit default constructor, it is provided for you by the compiler.

for example say I left the constructor out, then i just created the objects by doing the following:



then that would be using the default constructor. if you add no constructors of your own then the compiler automatically adds the default constructor in for you.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kevin Althaus wrote:
dog1, dog2, dog3 are the objects created from the class Dog.


Actually, they're just variables that hold references that "point to" the Dog objects. Often times as shorthand we'll talk about variables and the objects they point to interchangeably, but it's important to understand the distinction.

The methods are used to construct the object. Would it be safe to say that the class is just a container for the methods?


Not really, no. As I stated, the class defines how its objects behave and what state (data) they store.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Also, note that constructors don't actually create the objects. That's done by the new operator. The constructor's job is to put the newly created object into a valid initial state.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Jeff is very much correct, they are reference variables pointing to those objects, and like he says it is easy to forget the difference. so like dog1 is a reference variable of type Dog pointing to the Dog object created on the heap.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Methods and Classes
 
Similar Threads
Bodgitt and Scarper : locking and unlocking
Thread class
Subtract 2 Dates
final
why wait notify notifyall defined in object class