-3
public class Person {
    private int age;    

    public Person(int initialAge) {
     if (initialAge<= 0) {
         System.out.println("Age is not valid, setting age to 0.");
     }
      else {
          age = initialAge;
      }
    // Add some more code to run some checks on initialAge
    }

  public void amIOld() {
    if (age < 13) {
        System.out.print("You are young.");

    }
    else if (age >= 13 && age < 18) {
        System.out.print("You are a teenager.");
    }
    else {
        System.out.print("You are old.");
    }
    // Write code determining if this person's age is old and print the correct statement:
    System.out.println(/*Insert correct print statement here*/);
  }

  public void yearPasses() {
    age += 1;

  }




  public static void main(String[] args  {

    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    for (int i = 0; i < T; i++) {
        int age = sc.nextInt();
        Person p = new Person(age);
        p.amIOld();
        for (int j = 0; j < 3; j++) {
            p.yearPasses();
        }
        p.amIOld();
        System.out.println();
    }
    sc.close();
  }
}

In the code above, when an instance of the person class is created with a parameter, does it automatically call the Person method within the class?

is the code Person p = new Person(age); a constructor or a method call? is it both? What is the purpose of having a method with the same name as the class? How does it function?

Prim
  • 2,880
  • 2
  • 15
  • 29
Zeboon
  • 31
  • 1
  • 3
  • 7
    Look up "constructor". That is what this "method with the same name as the class" is. – Thomas Padron-McCarthy Aug 03 '17 at 08:54
  • Conceptually, a constructor is not a method, even though it looks a lot like one. Think of a constructor as a special block of code that is called to initialize a new object. – Jesper Aug 03 '17 at 09:31

4 Answers4

2

I, personally, do not like a method with the same nam as the class it is in, but:

public class Person {
  public Person() {}    // constructor
  public void Person {} // method
  public static void main(String... args) {
    Person p = new Person();
    p.Person();
  }
}

Generally, a class's name should be a noun, while a method's name should be a verb.

Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

That's called the constructor. It's axecuted when you create a new instance like this: Person me = new Person(99); You can have multiple constructors like:

enter code here
public Person(int initialAge) {
    if (initialAge<= 0) {
        System.out.println("Age is not valid, setting age to 0.");
    }
    else {
        age = initialAge;
    }
}

public Person() {
    age = 0;
}

in the same class. In that case if you create an instance like this: like this: Person me = new Person(99); only the first constructor is called and if you create an instance like this: Person me = new Person(); only the second one (unless you have one referencing the other of course).

Lavandysh
  • 531
  • 1
  • 7
  • 15
0

A mehtode named like the class and without void or any returntyp is called a constructor.

class MyClass {
    public MyClass () {
        //constructor code here
    }
}

Every calss has a default constructor (showen above) witch does not have to be expilcitly written out to be used (generated by the compiler) but it can be overritten and overloaded for more functunallity. A constructor is called everytime when an object of the class is generated.

class Person {
    pulbic int age;

    public Person (int age) {
        this.age = age;
  }


   }

class Main {
    public static void main(String args[]) {
        //creating 2 objects of person
        Person p1 = new Person(23)
        Person p2 = new Person(51)
    }
}

This programm now generates 2 objects of person the objct p1 holds in the age variable the value 23 and the objct p2 holds in the age variable the value 51. The ages are set by the constructor called with the new Person(int age) statment.

If you have not initialized an class vriable (in this case int age) but set its value over a constructor, it is treated by the rest of the class as if it was initialized with a specific value.

You can force the programmer with constructors to set specific attributs of an class at its creation and can therfor be sure that this values are set.

If you overload constructors you can call another constructor with:

this(/*Overloaded parameters*/);

Constructors also can not call any methodes (exept other constructors) of the class, because the class is not fully generated a the time the constructor is running.

ProfBits
  • 199
  • 1
  • 9
  • A constructor implies its name same as its class, but not vice versa. I think we can have a normal method whose name same as its class. The difference is that a constructor *can't have* a return type (even if it's a `void`) while the normal methods *must* have a return type. CMIIW. – Hzzkygcs Feb 15 '21 at 11:50
-1

When an object of a class is created then

  1. Static block is executed first
  2. Constructor if not defined then default Constructor
  3. After that method called with that object.

Constructor is not a method as it does not have any return type.If constructor not defined in the class then default constructor is called automatically. Otherwise particular Constructor will be called when creating an object.

Sumit Kumar
  • 249
  • 1
  • 7