DEV Community

Abhay Prajapati
Abhay Prajapati

Posted on

4 2

OOPs Lecture 1

OOPs Lecture 1

learned about contructor's

  • here is what understand about contructor is that;
  • we can create object of a class
class Person{
    String name;
    int age;
}
Enter fullscreen mode Exit fullscreen mode

creating object of class Person

// 
Person p = new Person();
p.name = "John";
p.age = 30;
// so here as we have not created constutor's we need to write p.name; p.age;
Enter fullscreen mode Exit fullscreen mode
class Human {
    String name;
    int age;
    public Human(String name, int age){
        this.name = name;
        this.age = age;
    }
}
Enter fullscreen mode Exit fullscreen mode
Human h = new Human("John", 30);

// as we have created constuctor's we need not to write h.name; h.age;
// else we add that in the parameter of constructor's/fn
// contructors is a special function that is used to initialize the object

Enter fullscreen mode Exit fullscreen mode

pay attention to the this keyword;
it is nothing but the object itself;
this.name == h.name

under the hood the this keyword will be replaced by the object (h) itself

If we donot provide this then we need to always need to write h.name object name;

repo for code🧑🏻‍💻

🤝🏾Connect me on:

Twitter: 🕊️@Abhayprajapati_
Github: 🐧@theabhayprajapati


suggest and edit🖋️

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay