Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Objects, Constructors, Methods

Can some one help me to understand Methods, Constructors and Objects in java? Yes I have done the course but i still find it hard to understand and memorize how to make them. I have watched many videos and just need someone who UNDERSTANDS to please help me to understand. Please and Thank You.

1 Answer

I'm not sure if I can explain this to you properly but...

I always though of Objects as some kind of Data Type (int, double, char, etc.) but in Object we can specify what properties it may contain. (just think of it as your own Custom Data Type)

and Constructors is like setting a prerequisite to instantiate an Object and the methods is something your Objects can do.

// this is the Object
public Class Person {

private char mGender;
private int mAge;
private String mName;

  // This is the Constructor
  public Person(String name, int age, char gender) {
      mName = name;
      mAge = age;
      mGender = gender;
  }

  // This is a method
  public String greet() {
     return "Hello! My name is " + mName;
  }
}

in my example let's say you have Person object. We created the Person object so that we can have multiple instance of it.

example:

     Person person1 = new Person("John", 18, 'M');
     Person person2 = new Person("Mary", 19, 'F');

since Mary and John is a person, we created a Person Object. This will serve as a variable to hold the data that Mary and John will give to us and as you can see when we instantiated the Person Object we have "new Person("John", 18, 'M')" this is a work of constructor if we don't give the necessary parameters when instantiating the object there will be a compiler error.

Think of this way. The "Person" in real life scenario the person should have a name, age, and gender right? So the constructor is making sure that the person you're going to input should have a name, age, and gender.

Lastly, the method. The method serves as a something your object can do. Let's just say that a Person can at least greet in real life. So in the object Person I included the greet() method.

Now my Person Object can do greeting.

     Person person1 = new Person("John", 18, 'M');
     Person person2 = new Person("Mary", 19, 'F');

     person1.greet();
     person2.greet();

so in the console it will look like something like this.

   Hello! My name is John
   Hello! My name is Mary

you can always add more methods like sayAge() which will return the age of the person or something like that.

Hope this helps.