Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Method vs. Object  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between a method and an object? My professor touched a bit on this subject but I am still confused.
 
Sheriff
Posts: 12357
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of an Object in your program as something that has behavior and (maybe) data on which it bases that behavior.

Think of a Method as a behavior (or capability) that an object can have.

For example,

Can you tell me which is the object, the behavior, and the data that the behavior uses?

For more about methods and objects, consult the Official Java Tutorials
 
Marshal
Posts: 60199
188
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects ARE things and methods DO things usually with objects. As Junilu says, most objects contain methods which are parts (=members) of those objects.
 
Junilu Lacar
Sheriff
Posts: 12357
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the code for that simple example would typically be:
 
Leslee Rivera
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
Can you tell me which is the object, the behavior, and the data that the behavior uses?



Hi Junilu,

I think the object is Person and the method is sayName(). I'm guessing the behavior the object uses is the String name? I'm not sure.

Thank you for your responses to my questions.

Leslee
 
Campbell Ritchie
Marshal
Posts: 60199
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting better
Person is the name of the class the object is made from.
sayName is the method (correct).
But name is not a behaviour; it is a datum. Please have another look at Junilu's example. When you get the right answer, you will be surprised by how simple is looks.
 
Leslee Rivera
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. I don't understand why this is so difficult for me to understand.
What I understand is that sayName is a method because it says what the object will do, which means the object will be this.name? If so then the object is a String that the method sayName() says it will print out as "Hello, my name is Leslee Rivera" because in the main a new object is created (new Person) being Leslee Rivera?

Thank you for your time,

Leslee

OH! I think I get it! The object is you, where Person you = new Person()
 
Saloon Keeper
Posts: 9257
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects consist of other objects.

A Person has-a name. A Person is an object. A name is an object.

In Junilu's first example, 'you' refers to one Person object. Inside the Person class, the 'this' keyword also refers to a Person object.

sayName() is a method that prints out a String. Which one? The one referred to by the name variable of this Person object.

In Java, it's very important to realize the difference between references and objects.

Objects exist. Somewhere. Variables hold references to objects. With the 'new' keyword, you can create a new object of some class, and assign a reference to that object to a variable:
 
Campbell Ritchie
Marshal
Posts: 60199
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
More precisely “you” is a reference to the object. As I said, these things become simpler when you find the answer.
Have you worked out what the behaviour is? Once you get it, you can describe it in about 3 or 4 words. Stephan took longer than that to describe it
 
Leslee Rivera
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects are something or an entity that are defined by a class, so what it looks like and what it does.

In this case, the class Person has a set of properties that make up the qualities of the object, so I can make many Person objects from the Person class.

The behavior for the object is defined in the method sayName(), which is the System.out.print("Hello my name is " + this.name);

Yes? (I hope)

Thank you and apologies for the many questions,

Leslee
 
Stephan van Hulst
Saloon Keeper
Posts: 9257
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that looks about right.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!