DEV Community

Jack Pritom Soren
Jack Pritom Soren

Posted on

5 2

Method and Attribute in OOP

Method :

A method is the equivalent of a function in object-oriented programming. A noun is to a verb what a variable is to a method — the methods are the actions that perform operations on a variable. A method accepts parameters as arguments, manipulates these, and then produces an output when the method is called on an object. Methods are similar to functions, but methods are also classified according to their purpose in the class design. In classes, variables are called attributes, so methods often operate on attributes.

public class Main {
  static void myMethod() { //myMethod() is the name of the method
    // code to be executed
  }
}
Enter fullscreen mode Exit fullscreen mode

Calling Method :

public class Main {
  static void myMethod() {
    System.out.println("From Method");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

// Outputs "From Method"
Enter fullscreen mode Exit fullscreen mode

Attribute :

Attributes are data members inside a class or an object that represent the different features of the class. They can also be referred to as characteristics of the class that can be accessed from other objects or differentiate a class from other classes.

public class Main {
  int x = 1;
  int y = 2; 
  // Create a class called "Main" with two attributes: x and y
}
Enter fullscreen mode Exit fullscreen mode

Image of Stellar post

From Hackathon to Funded - Stellar Dev Diaries Ep. 1 🎥

Ever wondered what it takes to go from idea to funding? In episode 1 of the Stellar Dev Diaries, we hear how the Freelii team did just that. Check it out and follow along to see the rest of their dev journey!

Watch the video

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay