• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Methods and Classes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the class creates the object. The class itself isn't the object.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic