File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods and Classes

 
Kevin Althaus
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the class creates the object. The class itself isn't the object.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic